Home > Net >  Can't match word with following \ and all symbols after in my regular expression
Can't match word with following \ and all symbols after in my regular expression

Time:11-04

I need match next cases:

no word\ on your
no word\on your
no word\
no word

ignoring :

no word on your

My regular expression doesn't cover my needs

^.*(?=\bword\b)(?:\\.*|)$

To verify results you can use prepared configuration.

...Looking for help.

CodePudding user response:

Your positive lookahead is followed with \\.* optional pattern followed with end of string, so the word string should either start at the same location as \ does or at the end of string, which means, your regex will never match any text.

You can use

^.*\bword(?:\\.*)?$

See the regex demo. There is no need adding another \b as \ after word is already a non-word char, and if the optional \\.* pattern does not exist, the end of string is already acting as a trailing word boundary for word.

Details:

  • ^ - start of string
  • .* - zero or more chars other than line break chars as many as possible
  • \b - a word boundary
  • word - a word
  • (?:\\.*)? - an optional non-capturing group matching \ and then any zero or more chars other than line break chars as many as possible
  • $ - end of string.
  • Related