Search code examples
python-3.xregexpython-re

re.search in python, return a list of integers separated by comma


I have a list of strings as follows:

my_list = ['array("i", [12694499, 12855016, 123457])',
'array("i", [12694499, 12865016, 123457])',
'array("i", [12694499, 12855016])',
'array("i", [12699749, 12877477])',
'array("i", [12828285, 12868277])',
'array("i", [-1])',
'array("i", [-1])',
'array("i", [-1])']

I am a newbie using regular expressions in python and I'm trying to use re.search to extract the values in brackets and separated by commas. As follows:

[12694499, 12855016, 123457]
[12694499, 12865016, 123457]
[12694499, 12855016]
[12699749, 12877477]
[12828285, 12868277]
[-1]
[-1]
[-1]

I have tried this:

for value in my_list:
    coords = re.search(r"\[[0-9]+,\s[0-9]+\]", value)
    print(coords)

but I get this:

None
None
None
None
None
None
None
None

I know I can use split() to get the numbers, but I'm really interested in understand how RE works in python.

Any suggestion would be much appreciated!


Solution

  • Your code actually has a match for the 3rd, 4th and 5th item.

    But if you want to get all the matches, you can use an optional hyphen -? and optionally repeat the part with the comma and the digits (?:,\s-?[0-9]+)*

    Then first check the result of re.search and print the value with .group()

    for value in my_list:
        coords = re.search(r"\[-?[0-9]+(?:,\s-?[0-9]+)*]", value)
        if coords:
            print(coords.group())
    

    Output

    [12694499, 12855016, 123457]
    [12694499, 12865016, 123457]
    [12694499, 12855016]
    [12699749, 12877477]
    [12828285, 12868277]
    [-1]
    [-1]
    [-1]
    

    See a Python demo.