Reputation: 1721
I thought [^0-9a-zA-Z]*
excludes all alpha-numeric letters, but allows for special characters, spaces, etc.
With the search string [^0-9a-zA-Z]*ELL[^0-9A-Z]*
I expect outputs such as
ELL
ELLs
The ELL
Which ELLs
However I also get following outputs
Ellis Island
Bellis
How to correct this?
Upvotes: 1
Views: 477
Reputation: 626851
You may use
(?:\b|_)ELLs?(?=\b|_)
See the regex demo.
It will find ELL
or ELLs
if it is surrounded with _
or non-word chars, or at the start/end of the string.
Details:
(?:\b|_)
- a non-capturing alternation group matching a word boundary position (\b
) or (|
) a _
ELLs?
- matches ELL
or ELLs
since s?
matches 1 or 0 s
chars(?=\b|_)
- a positive lookahead that requires the presence of a word boundary or _
immediately to the right of the current location.Upvotes: 1
Reputation: 5274
change the * to +
a * means any amount including none. A + means one or more. What you probably want though is a word boundry:
\bELL\b
A word boundry is a position between \w and \W (non-word char), or at the beginning or end of a string if it begins or ends (respectively) with a word character ([0-9A-Za-z_]). More here about that: What is a word boundary in regexes?
Upvotes: 1