Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a regex to match if any pattern appears once out of many times in a given sequence

Hard to word this correctly, but TL;DR.

I want to match, in a given text sentence (let's say "THE TREE IS GREEN") if any space is doubled (or more).

Example:

"In this text,
THE TREE IS GREEN should not match,
THE  TREE IS GREEN should
and so should THE  TREE   IS GREEN
but  double-spaced  TEXT  SHOULD  NOT BE  FLAGGED outside the pattern."

My initial approach would be

/THE( {2,})TREE( {2,})IS( {2,})GREEN/

but this only matches if all spaces are double in the sequence, therefore I'd like to make any of the groups trigger a full match. Am I going the wrong way, or is there a way to make this work?

like image 488
Alessandro Jeanteur Avatar asked Jul 08 '19 19:07

Alessandro Jeanteur


People also ask

How do I match a pattern in regex?

2.1 Matching a Single Character The fundamental building blocks of a regex are patterns that match a single character. Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" .

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

Is used for zero or more occurrences in regex?

A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used.


1 Answers

You can use Negative lookahead if there is an option.

First match the sentence that you want to fail, in your case, it is "THE TREE IS GREEN" then give the most generic case that wants to catch your desired result.

(?!THE TREE IS GREEN)(THE[ ]+TREE[ ]+IS[ ]+GREEN)

https://regex101.com/r/EYDU6g/2

like image 83
karthick Avatar answered Sep 30 '22 19:09

karthick