简体   繁体   中英

RegEx multiple optional characters in group

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM