Home > OS >  I don't want match abc with spaces AT ALL if followed by some other non space characters
I don't want match abc with spaces AT ALL if followed by some other non space characters

Time:04-11

I use this regex https://regex101.com/r/7Cw1fy/1

abc(\s*)(?![\S] )

abc matches -> OK

abc cde matches -> not OK

CodePudding user response:

You can use

abc(?!\s*\S)(\s*)

See the regex demo. Details:

  • abc - a fixed string
  • (?!\s*\S) - a negative lookahead that fails the match if there are zero or more whitespaces followed with a whitespace immediately to the right of the current location
  • (\s*) - Group 1: zero or more whitespaces.

CodePudding user response:

Why not use: abc\s*$ ?

This will match only if there are no character other than a whitespace following 'abc'.

Test here: https://regex101.com/r/HdJ0FS/1

abc             -> matches
abc cde         -> no match
abc             -> match
abc      sdfds  -> no match
  • Related