user3616092
user3616092

Reputation: 21

regular expression: matching does not contain specific string

By using a regular expression I want to get all indexes where the following character sequence appears:

FORALL ... in ... : 

// between "FORALL" and "in" might be whitespaces and non-word characters like ","

example: find these 3 ones:

  1. FORALL i<=j in a1,x,y:
  2. FORALL i,j in a1,x,y:
  3. FORALL i <= j in a1:

replace with this one:

FORALL i,j in a1:

part of the idea:
the substring has to start with the word FORALL
the "in" respectively the ":" has to appear exactly one time - therefore do not get by mistake the "in" from the actual next independent match
inside of the substring is no "in" allowed but the first part should end with one

My last trying is the following regular expression

"(^FORALL[<=|>=|<|>|==|!=](?!.*in).*in$)((?!.*\\:).*\\:$ )"

according: Regular expression that doesn't contain certain string

Upvotes: 0

Views: 115

Answers (1)

Jerry
Jerry

Reputation: 71578

You could maybe give this regex a try? (it's a literal string)

^(FORALL [^<=>,: ]+) *[<=>,]+ *([^<=>,: ]+)\s+in\s+([^,:]+)[^:]*:$

Here's a breakdown:

^             # Beginning of string
(             # 1st capture begins
  FORALL      # Match FORALL and a space
  [^<=>,: ]+  # Any characters except <=>,: or space
)             # 1st capture ends
 *            # Any spaces
[<=>,]+ *     # Any characters of <=>, followed by any spaces
(             # 2nd capture begins
  [^<=>,: ]+  # Any characters except <=>,: or space
)             # 2nd capture ends
 +in +        # Match in surrounded by spaces
([^,:]+)      # Match any non , or : characters
[^:]*:        # Match any non : characters, then match a :
$             # End of string

And replace with:

$1,$2 in $3:

regex101 demo

Upvotes: 2

Related Questions