Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - capture only if group exists, if not ignore

Tags:

regex

I have this regex

(\w+)\s*\:\s*(\W\d+)

set to match something like

"price: $200"

what do I need to do to change it so it will match either "price: $200" or "$200" (if the price: part doesn't exist)

I tried

(?:(\w+)\s*\:\s*)*(\W\d+)

but that doesn't work, as it gives me an empty match for the first one

like image 789
Nick Ginanto Avatar asked Sep 04 '14 13:09

Nick Ginanto


People also ask

How do I capture a group in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d", "o", and "g".

What is the point of non capturing group in regex?

They can help you to extract exact information from a bigger match (which can also be named), they let you rematch a previous matched group, and can be used for substitutions.

How does group work in regex?

What is Group in Regex? A group is a part of a regex pattern enclosed in parentheses () metacharacter. We create a group by placing the regex pattern inside the set of parentheses ( and ) . For example, the regular expression (cat) creates a single group containing the letters 'c', 'a', and 't'.

What is capturing group in regex?

Capturing group. (regex) Parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex. (abc){3} matches abcabcabc. First group matches abc.

What are non-capturing parentheses in regex?

Non-capturing parentheses group the regex so you can apply regex operators, but do not capture anything. (?:abc){3} matches abcabcabc. No groups. Substituted with the text matched between the 1st through 9th numbered capturing group.

How to capture multiple groups in Python using regex?

Python Regex Capturing Groups 1 Example to Capture multiple groups. To extract the uppercase word and number from the target string we must first write two regular expression patterns. 2 Regex capture group multiple times. In earlier examples, we used the search method. ... 3 Extract range of groups matches. ...

What is the use of grouping in regex?

They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex.


2 Answers

how about:

(\w+\s*:\s*)?(\W\d+)

the ? means the group will show up 1 or 0 time. you can add your own capture group(s) for convenient data fetching.

like image 104
Kent Avatar answered Oct 14 '22 01:10

Kent


(?=.*?:)(\S+)\s*:\s*(\S+)|(\S+)

You can use lookeahead as well.

See demo.

http://regex101.com/r/oO8zI4/5

like image 22
vks Avatar answered Oct 14 '22 02:10

vks