Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find best match from regex capture group

Tags:

.net

regex

I'd like to find the best match with a given regex-capturegroup. Example:

Regex: (AB|ABC)

Sample: ABCDEF

Result: AB

But I'd like to find ABC (the best fit from the option-list).

How can this be done? (except of exchanging the both alternatives in the group)?

Thanks for any idea!

like image 202
Sascha Avatar asked Oct 13 '22 18:10

Sascha


1 Answers

Try specifying your best matches in order (e.g., longest strings first). Many RE engines prefer left-most first in their "eager evaluation" mode. Only POSIX REs mandate that the longest matching alternate be returned:

http://www.regular-expressions.info/alternation.html

like image 151
kvista Avatar answered Nov 15 '22 13:11

kvista