Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make part of a regex match optional?

Tags:

regex

This is an example string:

123456#p654321 

Currently, I am using this match to capture 123456 and 654321 in to two different groups:

([0-9].*)#p([0-9].*) 

But on occasions, the #p654321 part of the string will not be there, so I will only want to capture the first group. I tried to make the second group "optional" by appending ? to it, which works, but only as long as there is a #p at the end of the remaining string.

What would be the best way to solve this problem?

like image 282
user1447941 Avatar asked Sep 17 '12 00:09

user1447941


People also ask

How can I make part of regex optional?

To make the . + optional, you could do: \"(?:. +)?

What is ?! In regex?

The ?! n quantifier matches any string that is not followed by a specific string n.

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 match line breaks in regex?

If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.


1 Answers

You have the #p outside of the capturing group, which makes it a required piece of the result. You are also using the dot character (.) improperly. Dot (in most reg-ex variants) will match any character. Change it to:

([0-9]*)(?:#p([0-9]*))? 

The (?:) syntax is how you get a non-capturing group. We then capture just the digits that you're interested in. Finally, we make the whole thing optional.

Also, most reg-ex variants have a \d character class for digits. So you could simplify even further:

(\d*)(?:#p(\d*))? 

As another person has pointed out, the * operator could potentially match zero digits. To prevent this, use the + operator instead:

(\d+)(?:#p(\d+))? 
like image 139
Jonah Bishop Avatar answered Sep 18 '22 17:09

Jonah Bishop