Maverick
Maverick

Reputation: 1434

Regex - No match if trailing character is "-"

I have a regex as below

^Schedule\s?(A|B|C|D|E|F|H|J|K|L|M|R|SE)?

so this will match anything like "Schedule A i need help".

I want restriction for the character "-" i.e it should not give a match if the string is like "Schedule A - i need help" .

But it should give a match if Schedule A is followed by anything other than a space and "-".

Upvotes: 2

Views: 1057

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26677

Negative look aheads will be helpful here

^Schedule\s*([ABCDEFHJKLMR]|SE)(?!\s+-)
  • (?!\s+-) Negative look ahead, checks if the matches string is not followed by space (\s+) and the -.

  • Note The optional quantifiers ? are not required as it causes the regex engine to skip them.

  • [ABCDEFHJKLMR] Character class, matches a single character from this set.

Regex Demo

Upvotes: 3

Related Questions