Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex : Find a number between parentheses

Tags:

I need a regex who find the number in bold below :

20 (L.B.D.D. hello 312312) Potato 1651 (98)

20 (L.B.D.D. hello 312312 bunny ) Potato 1651 (98)

20 (312312) Potato 1651 (98)

((\d+)) find the number 98

I do not know what to do when there are other characters in the parenthesis

like image 480
jonlabr Avatar asked Dec 10 '12 18:12

jonlabr


People also ask

What is difference [] and () in regex?

[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

How do you use parentheses in regex?

By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex. Only parentheses can be used for grouping.

How do I check a number in regex?

The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9. Tip: Use the [^0-9] expression to find any character that is NOT a digit.

How do I get out of parentheses in regex?

Python Regex Escape Parentheses () You can get rid of the special meaning of parentheses by using the backslash prefix: \( and \) . This way, you can match the parentheses characters in a given string.


1 Answers

This only matches 312312 in the first capture group:

^.*?\([^\d]*(\d+)[^\d]*\).*$ 

Regexplanation:

^        # Match the start of the line .*?      # Non-greedy match anything \(       # Upto the first opening bracket (escaped) [^\d]*   # Match anything not a digit (zero or more) (\d+)    # Match a digit string (one or more) [^\d]*   # Match anything not a digit (zero or more) \)       # Match closing bracket .*       # Match the rest of the line $        # Match the end of the line 

See it here.

like image 197
Chris Seymour Avatar answered Oct 12 '22 16:10

Chris Seymour