Regex : Find a number between parentheses

后端 未结 2 1858
执笔经年
执笔经年 2020-12-25 13:37

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 bu

相关标签:
2条回答
  • 2020-12-25 14:14

    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.

    0 讨论(0)
  • 2020-12-25 14:18

    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.

    0 讨论(0)
提交回复
热议问题