I have an input string which is a directory address:
Example: ProgramFiles/Micro/Telephone
And I want to match it against a list of words very strictly:
Example: Tel|Tele|Telephone
I want to match against Telephone
and not Tel
. Right now my regex looks like this:
my( $output ) = ( $input =~ m/($list)/o );
The regex above will match against Tel
. What can I do to fix it?
If you want a whole word match:
\b(Tel|Tele|Telephone)\b
\b
is a zero-width word boundary. Word boundary in this case means the transition from or to a word character. A word character (\w
) is [0-9a-zA-Z_]
.
If you simply want to match against the longest in a partial word match put the longest first. For example:
\b(Telephone|Tele|Tel)
or
(Telephone|Tele|Tel)
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