chess123
chess123

Reputation: 23

Regular expression for printing integers within brackets

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 in a string inside bracket?

Example:

dijdi[d43]     d5[55++][ 43]  [+32]dm dij [    -99]x

would return

[43, 32, -99]

'+' and '-' is okay, if it's in the beginning of the brackets, but not okay if it's in the middle or end. If the '+' sign is in the beginning, it should not be taken into account. (+54 --> 54)

Been trying :

re.findall('\[[-]?\d+\]',str)

but it's not working the way I want.

Upvotes: 2

Views: 303

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626919

If you need to fail the match in [ +-34 ] (i.e. if you needn't extract a negative number if there is a + before it) you will need to use

\[\s*(?:\+|(-))?(\d+)\s*]

and when getting a match, concat the Group 1 and Group 2 values. See this regex demo.

Details

  • \[ - a [ char
  • \s* - 0+ whitespaces
  • \+? - an optional + char
  • (-?\d+) - Capturing group 1 (the actual output of re.findall): an optional - and 1+ digits
  • \s* - 0+ whitespaces
  • ] - a ] char.

In Python,

import re
text = "dijdi[d43]     d5[55++][ 43]  [+32]dm dij [    -99]x"
numbers_text = [f"{x}{y}" for x, y in re.findall(r'\[\s*(?:\+|(-))?(\d+)\s*]', text)]
numbers = list(map(int, numbers_text))

# => [43, 32, -99] for both

Upvotes: 1

Cary Swoveland
Cary Swoveland

Reputation: 110685

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 ']' 

Upvotes: 0

user13855328
user13855328

Reputation:

If you want to extract integers from a string the code that I use is this:

def stringToNumber(inputStr):
myNumberList = []
for s in inputStr.split():
    newString = ''.join(i for i in s if i.isdigit())
    if (len(newString) != 0):
        myNumberList.append(newString)
return myNumberList

I hope it works for you.

Upvotes: 0

Related Questions