RegEx multiple optional characters in group -


how define optional characters in group?

i trying match following...

kg kilo kilos kilogram kilograms g gram grams 

i know can put them individually in group, wondering if fancy this...

(kg|kilo?g?ram?s?) 

problem match s? or none of second alternation match 0 length.

i start enumerating of possible match conditions , paring down there see if there more efficient solution:

kg|kilo|kilos|kilogram|kilograms|g|gram|grams 

the plural 's' obvious redundancy:

kg|kilos?|kilograms?|g|grams? 

g , kg can collapsed:

k?g|kilos?|kilograms?|grams? 

we can collapse units kilograms:

k?g|kilo(?:s|grams?)?|grams? 

are ok 6 character duplication of "grams?" :)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -