Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression Longest Possible Matching

Tags:

regex

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?

like image 421
syker Avatar asked Mar 30 '10 22:03

syker


1 Answers

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)
like image 156
cletus Avatar answered Sep 19 '22 07:09

cletus