sol
sol

Reputation: 93

Matching all non whitespace characters after a string in Regex

I'm trying to match all the non whitespace characters after a string in Regex. In this example, I want to match "b" without the whitespaces and the slashes around it:

      a:     /b/     

I tried using (?<=a:)([^\s\/]+) but it doesn't work.

Upvotes: 2

Views: 55

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626729

You still need to account for / before b, not just for whitespace.

You may use a \K based regex (if your regex flavor is PCRE/Onigmo/Boost):

a:\s*\/\K[^\s\/]+

See the regex demo.

Also, if you are using a regex engine that supports unknown width lookbehind patterns, you may use

(?<=a:\s*\/)[^\s\/]+

See this regex demo.

Else, you need to capture your substring with parentheses:

a:\s*\/([^\s\/]+)

See this regex demo.

Details

  • a: - a a: string
  • \s* - 0+ whitespaces
  • \/ - a / char
  • \K - a match reset operator
  • [^\s\/]+ - 1+ chars other than whitespace and /.

Upvotes: 2

Related Questions