Search code examples
pythonstringmatchpython-re

Reading strings in python


I am using python to try to separate the information from strings of this type:

r = "(-0.04530261550379927+0j) [X0 X1 Y2 Y3]"

ultimately the information that I need is the number in the parenthesis and separate the letters from the numbers in the array. So in the example above, the results that I would like to get are: the number -0.04530261550379927, an array: [X, X, Y, Y] and another array: [0, 1, 2, 3].

I have been trying with re.match but since this is the first time that I use this module I find it very confusing.

Would appreciate any help.


Solution

  • You can do like this:

    import re
    
    r = "(-0.04530261550379927+0j) [X0 X1 Y2 Y3]"
    match = re.match(r"\(([-+]?\d+(?:\.\d+)?)\+\d+j\) \[((?:[XYZ]\d(?: [XYZ]\d)*)?)]", r)
    number, array = match.groups()
    
    number = float(number)
    a1, a2 = [], []
    for i in array.split():
        a1.append(i[0])
        a2.append(int(i[1]))
    
    print(number, a1, a2)
    

    Explanation:

    Regex pattern r"\(([-+]?\d+(?:\.\d+)?)\+\d+j\) \[((?:[XYZ]\d(?: [XYZ]\d)*)?)]" matches the given string:

    • part ([-+]?\d+(?:\.\d+)?) matches number
    • part ((?:[XYZ]\d(?: [XYZ]\d)*)?) matches array
    • there are non-capturing groups defined like (?:<match>)

    match.groups() returns a list of all captured groups (2 in our case), and we unpack the list to variables number, array

    Next, we split our string stored in array by space and iterate through items:

    • first character is appended to a1
    • second character is converted to int and appended to a2

    Output:

    -0.04530261550379927 ['X', 'X', 'Y', 'Y'] [0, 1, 2, 3]