Regex in python for positive and negative integer
<p>I am new to learning regex in python and I'm wondering how do I use regex in python to store the integers(positive and negative) i want into a list!</p>

<p>For example</p>

<p>This is the data in a list.</p>

<pre><code>data =
    [u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=-5,B=5)', 

    u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=5,Y=5)', 

    u'\x1b[0m[\x1b[1m\x1b[10m\xbb\x1b[0m\x1b[36m]\x1b[0m : ']
</code></pre>

<p>How do I extract the integer values of A and B (negative and positive) and store them in a variable so that I can work with the numbers?</p>

<p>I tried smth like this but the list is empty .. </p>

<pre><code>for line in data[0]: 
        pattern = re.compile("([A-Z]=(-?\d+?),[A-Z]=(-?\d+?))") 
        store = pattern.findall(line)

print store
</code></pre>

<p>Thank you and appreciate it </p>

<p>Depending on what you are trying to accomplish, this may work:</p>

<pre><code>import re

data = [
    u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=-5,B=5)',
    u'\x1b[0m[\x1b[1m\x1b[0m\xbb\x1b[0m\x1b[36m]\x1b[0m (A=5,Y=5)',
    u'\x1b[0m[\x1b[1m\x1b[10m\xbb\x1b[0m\x1b[36m]\x1b[0m : '
]

for line in data:
    m = re.search('\((\w)=(-?\d+),(\w)=(-?\d+)\)', line)
    if not m:
            continue
    myvars = {}
    myvars[m.group(1)] = int(m.group(2))
    myvars[m.group(3)] = int(m.group(4))
    print myvars
</code></pre>

<p>This results in a dictionary (<code>myvars</code>) containing the variables in the current line. If you use this, you will have to check that the variable you want is defined before you attempt to get it from the dictionary. The output of the above is:</p>

<pre><code>{u'A': -5, u'B': 5}
{u'A': 5, u'Y': 5}
</code></pre>

python regex