Regular expression for printing integers within brackets

前端 未结 3 1528
隐瞒了意图╮
隐瞒了意图╮ 2021-01-21 18:35

First time ever using regular expressions and can\'t get it working although there\'s quite a few examples in stackoverflow already.

How can I extract integers which are

3条回答
  •  野的像风
    2021-01-21 19:31

    If you've not done so I suggest you switch to the PyPI regex module. Using it here with regex.findall and the following regular expression allows you to extract just what you need.

    r'\[ *\+?\K-?\d+(?= *\])'
    

    regex engine <¯\(ツ)> Python code

    At the regex tester pass your cursor across the regex for details about individual tokens.

    The regex engine performs the following operations.

    \[       : match '['
    \ *      : match 0+ spaces
    \+?      : optionally match '+'
    \K       : forget everything matched so far and reset
               start of match to current position
    -?       : optionally match '-'
    \d+      : match 1+ digits
    (?= *\]) : use positive lookahead to assert the last digit
             : matched is followed by 0+ spaces then ']' 
    

提交回复
热议问题