How would I define optional characters in a group?
I am trying to match the following...
kg
kilo
kilos
kilogram
kilograms
g
gram
grams
I know I can put them individually in a group, but was wondering if I could do something fancy like this...
(kg|kilo?g?ram?s?)
Problem is it could match only the s? or none of the second alternation so it would match zero length.
I would start by enumerating all of the possible match conditions and then paring down from there to see if there is a more efficient solution:
kg|kilo|kilos|kilogram|kilograms|g|gram|grams
the plural 's' is an obvious redundancy:
kg|kilos?|kilograms?|g|grams?
g and kg can be collapsed:
k?g|kilos?|kilograms?|grams?
We can collapse the units for kilograms:
k?g|kilo(?:s|grams?)?|grams?
Are you OK with the six character duplication of "grams?" :)
You can use (?:)
to group items without capturing (this works in most RegEx flavours; look up "non-capturing groups" in your engine's documetation if you are unsure).
With that, you can try something like this:
(k?g|(?:kilo)?grams?|kilos?)
This matches exactly
g kg gram grams kilogram kilograms kilo kilos
and nothing else.
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