Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx multiple optional characters in group

Tags:

regex

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.

like image 506
Daniel Avatar asked Jun 20 '15 09:06

Daniel


2 Answers

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?" :)

like image 189
nexus Avatar answered Sep 29 '22 17:09

nexus


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.

like image 27
nneonneo Avatar answered Sep 29 '22 16:09

nneonneo