Alan
Alan

Reputation: 311

Regex to find a starting pattern including either of 2 strings but not contain a specific text

I want to use Regex to find a line containing a particular pattern.

The pattern should be a string starting with 2 characters (a-zA-Z0-9) followed by a dash then either "FAL" or "SAL" and does not include the term "OJT" at all.

Just want to make sure I have the right or am I missing something as it doesn't appear to work as expected

^[a-zA-z0-9]{1,2}(?=.*?\-SAL|-FAL\b)((?!OJT).)*$

Upvotes: 1

Views: 118

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627077

You may use

^[a-zA-Z0-9]{1,2}(?!.*OJT).*?(?:-SAL|-FAL)\b.*

See the regex demo

Details

  • ^ - start of string
  • [a-zA-Z0-9]{1,2} - one or two alphanumeric chars
  • (?!.*OJT) - any 0+ chars, as few as possible, followed with OJT char sequence should not appear immediately to the right of the current location
  • .*? - any 0+ chars other than line break chars as few as possible
  • (?:-SAL|-FAL)\b - -SAL or -FAL not followed with a word char
  • .* - the rest of string.

See the regex graph:

enter image description here

Upvotes: 1

Related Questions