I would like to match the whole "word"—one that starts with a number character and that may include special characters but does not end with a '%'.
Match these:
but not
I've tried these regular expressions:
(\b\p{N}\S)*)
but that returns '12%' in '12%'
(\b\p{N}(?:(?!%)\S)*)
but that returns '12' in '12%'
Can I make an exception to the \S
term that disregards %
?
Or will have to do something else?
I'll be using it in PHP, but just write as you would like and I'll convert it to PHP.
This matches your specification:
\b\p{N}\S*+(?<!%)
Explanation:
\b # Start of number
\p{N} # One Digit
\S*+ # Any number of non-space characters, match possessively
(?<!%) # Last character must not be a %
The possessive quantifier \S*+
makes sure that the regex engine will not backtrack into a string of non-space characters it has already matched. Therefore, it will not "give back" a %
to match 12
within 12%
.
Of course, that will also match 1!abc
, so you might want to be more specific than \S
which matches anything that's not a whitespace character.
Can i make an exception to the \S term that disregards %
Yes you can:
[^%\s]
See this expression \b\d[^%\s]*
here on Regexr
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With