JohnnyLoo
JohnnyLoo

Reputation: 927

Regular expression to accept missing word

I am trying to write a regexp that matches a sentence. Here are the three sentences I want to match

1) Volume High. My Example
2) Volume Low. My Example
3) Volume. My Example

I do not now how to represent the missing word in the following regexp

^Volume\s(High|Low|)\.\sMy Example$

The regexp above only matches sentence 1 and 2, but not 3. How can I make it work?

Thank you

Upvotes: 1

Views: 823

Answers (4)

JohnnyLoo
JohnnyLoo

Reputation: 927

Thank you guys. I see what you mean I ended up using the following

^Volume\s*(High|Low|)\.\sMy Example$

I like it because it is simpler. Thank you all for your help.

Upvotes: 1

Jongware
Jongware

Reputation: 22457

The suggested

^Volume\s(High|Low|)\.\sMy Example$

does not work on

Volume. My Example

because there is no space before the period, and the expression asks for it. Moving the space into the expression makes it work:

^Volume(\sHigh|\sLow|)\.\sMy Example$

Upvotes: 1

IMSoP
IMSoP

Reputation: 97718

To make a token in a regex optional (occurring 0 or 1 times), you add a ? after it.

The High and Low keywords are already grouped into one token, so (High|Low) would make them optional, but the space before would still be required.

You therefore have to choose either to make the space optional even if the next word matches...

^Volume\s?(High|Low)?\.\sMy Example$

...or create an extra group with the space in, so that the whole group can be optional...

^Volume(\s(High|Low|))?\.\sMy Example$

If you're capturing the value of the High|Low group, adding an extra group may be annoying; many regex implementations allow you to make a "non-capturing group" by adding ?: at the beginning, like this:

^Volume(?:\s(High|Low|))?\.\sMy Example$

Upvotes: 1

hwnd
hwnd

Reputation: 70732

You want to precede \s with the * operator meaning "zero or more" times and use an optional group.

^Volume\s*(High|Low)?\.\s*My Example$

Live Demo

Upvotes: 2

Related Questions