Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex optional group

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 
like image 652
joe Avatar asked Sep 05 '13 10:09

joe


People also ask

How do you specify an optional character in regex?

For example, the pattern ab? c will match either the strings "abc" or "ac" because the b is considered optional. Similar to the dot metacharacter, the question mark is a special character and you will have to escape it using a slash \? to match a plain question mark character in a string.

How do I make a group optional in regex python?

So to make any group optional, we need to have to put a “?” after the pattern or group. This question mark makes the preceding group or pattern optional. This question mark is also known as a quantifier.

How do you write a non-capturing group in regex?

What you want to do is to grab the first group of the match result, surrounded by () in the regex and the way to do this is to use the non-capturing group syntax, i.e. ?: . So the regex (\p{Alpha}*[a-z])(?:@example.com) will return just the id part of the email.


2 Answers

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

(?: ..... )? ^          ^____ optional |____ group 
like image 166
Daniel W. Avatar answered Oct 06 '22 14:10

Daniel W.


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.

like image 40
Jerry Avatar answered Oct 06 '22 13:10

Jerry