java - Regex - No match if trailing character is "-" -


i have regex below

^schedule\s?(a|b|c|d|e|f|h|j|k|l|m|r|se)? 

so match "schedule a need help".

i want restriction character "-" i.e should not give match if string "schedule - need help" .

but should give match if schedule followed other space , "-".

negative aheads helpful here

^schedule\s*([abcdefhjklmr]|se)(?!\s+-) 
  • (?!\s+-) negative ahead, checks if matches string not followed space (\s+) , -.

  • note optional quantifiers ? not required causes regex engine skip them.

  • [abcdefhjklmr] character class, matches single character set.

regex demo


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -