Home > Enterprise >  Regex lookahead with unknown number of spaces
Regex lookahead with unknown number of spaces

Time:06-19

I am trying to capture a string that can contain any character but must always be followed by ';' I want to capture it and trim the white space around it. I've tried using positive lookahead but that does not seem to exclude the whitespace. Example:

this is a match   ;
this is not a match

regex:

. (?=\s*;)

result:

"this is a match " gets captured with trailing white space behind.

expected result:

"this is a match" (without whitespace)

CodePudding user response:

You have to make sure the last characters of your match are not spaces. Thus the \S after the all character match (. ):

.*\S(?=\s*;)

Demonstration

Thanks to @CarySwoveland for improving the answer.

CodePudding user response:

You can match

.*(?<!\s)(?=\s*;)

provided the regex engine supports negative lookbehinds.

Demo

CodePudding user response:

You can make the dot non greedy and start the match with a non whitespace character:

\S.*?(?=\s*;)

Regex demo

If the non whitespace character itself should also not be a semicolon:

[^\s;].*?(?=\s*;)
  • Related