141

I am using this regex:

((?:[a-z][a-z]+))_(\d+)_((?:[a-z][a-z]+)\d+)_(\d{13})

to match strings like this:

SH_6208069141055_BC000388_20110412101855

separating into 4 groups:

SH
6208069141055
BC000388
20110412101855

Question: How do I make the first group optional, so that the resulting group is a empty string?
I want to get 4 groups in every case, when possible.

Input string for this case: (no underline after the first group)

6208069141055_BC000388_20110412101855
0

2 Answers 2

246

Making a non-capturing, zero to more matching group, you must append ?.

(?: ..... )?
^          ^____ optional
|____ group
Sign up to request clarification or add additional context in comments.

4 Comments

what is the difference between option group and (...)*
@GoldenLion ? matches "zero or one times" and * matches "zero or many times"
What does the ?: at the start of the group do?
@brobers a normal group (...) will capture the content in the results, but (?:...) makes it a non-capturing group, so it must match (or apply) but will not be included in the resulting groups.
101

You can easily simplify your regex to be this:

(?:([a-z]{2,})_)?(\d+)_([a-z]{2,}\d+)_(\d+)$
^              ^^
|--------------||
| first group  ||- quantifier for 0 or 1 time (essentially making it optional) 

I'm not sure whether the input string without the first group will have the underscore or not, but you can use the above regex if it's the whole string.

regex101 demo

As you can see, the matched group 1 in the second match is empty and starts at matched group 2.

2 Comments

Which of the characters is marking the group optional?
The ? after the first group? That's the 17th character from the left.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.