Reputation: 715
Trying to work out what the right RegEx would be for finding "s***" in a series of strings, e.g:
match for "find s*** in s*** foobar"
match for "s***"
don't match for "s******"
don't match for "s****** foobar"
I'm using a match because I want to count the number of instances of matches in the sentence. I was trying "s*{3}" as a starting point, and variations on $ and \b or \B but I can't quite figure it out.
I created some tests here to try it out, if that's helpful.
https://regex101.com/r/VdLyOY/2
Upvotes: 1
Views: 83
Reputation: 18641
Use
/\bs\*{3}\B(?!\*)/g
See proof
EXPLANATION
EXPLANATION
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
s 's'
--------------------------------------------------------------------------------
\*{3} '*' (3 times)
--------------------------------------------------------------------------------
\B the boundary between two word chars (\w)
or two non-word chars (\W)
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\* '*'
--------------------------------------------------------------------------------
) end of look-ahead
Upvotes: 0
Reputation: 786289
You may use this regex with a negative lookahead:
/\bs\*{3}(?!\*)/g
or with a positive lookahead:
/\bs\*{3}(?=\s|$)/g
RegEx Details:
\bs
: Match letter s
after a word bounday\*{3}
: Match *
3 times i.e. ***
(?!\*)
: Negative lookahead to assert that we don't have a *
ahead(?=\s|$)
: Positive lookahead to assert that we have a whitespace or line end at next positionUpvotes: 2
Reputation: 10414
/\bs\*{3}(\s|$)/g
might work depending on exactly what your criteria are.
Upvotes: 0