简体   繁体   中英

Regex : Find a number between parentheses

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

20 (LBDD hello 312312 ) Potato 1651 (98)

20 (LBDD 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

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 .

the following regex should do it

@"\([^\d]*(\d+)[^\d]*\)"

the parenthesis represent a capturing group, and the \\( are escaped parenthesis , which represent the actual parenthesis in your input string.

as a note: depending on what language you impliment your regex in, you may have to escape your escape char, \\ , so be careful of that.

I'd be careful with this though, One of the textbook limitations of regex is that it can't identify properly parenthesized text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM