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

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -