0

I am trying to match the following regex :

((symbol_live:)\s+\"(?P<LiveSymbol>.)\")?((symbol_dead:)\s+\"(?P<DeadSymbol>.)\")?(gamefield:\s*\n\"\n(?P<Seed>(((\g<DeadSymbol>)|(\g<LiveSymbol>))+\n)+)\"\s*)?

the given string to search in is :

gamefield:
"
-------------------------------
-------------------------------
----------oo-------------------
----------oo-------------------
------------oo-----------------
------------oo-----------------
-------------------------o-----
-------------------------o-----
-------------------------o-----
-------------------------------
-------------------------------
-------------------------------
-------------------------------
"
symbol_dead: "-"
symbol_live: "o"

I am writing this with python RE module and I used finditer to iterate through all the possible matches. it returns both symbol_dead: "-" and symbol_live: "o" but not the "seed" named group in gamefield

I write the following code to return the value for each group in the iteration:

p = re.finditer(r'((symbol_live:)\s+\"(?P<LiveSymbol>.)\")?((symbol_dead:)\s+\"(?P<DeadSymbol>.)\")?(gamefield:\s*\n\"\n(?P<Seed>(((\\g<DeadSymbol>)|(\\g<LiveSymbol>))+\n)+)\"\s*)?', string=input, flags=re.M)
for l in p:
    if 'DeadSymbol' in l.groupdict().keys() and l.groupdict()['DeadSymbol'] != None:
        print('DeadSymbol return = ' + l.groupdict()['DeadSymbol'])
    elif 'LiveSymbol' in l.groupdict().keys() and l.groupdict()['LiveSymbol'] != None:
        print('LiveSymbol return = ' + l.groupdict()['LiveSymbol'])
    elif 'Seed' in l.groupdict().keys() and l.groupdict()['Seed'] != None:
        print('Seed return = ' + l.groupdict()['Seed'])

When I check this in https://regexr.com/ it works! but not works in https://pythex.org/ I don't know what is wrong with the regex and I need help on this! since it is becoming frustrating for me to resolve!

12
  • When i try it on regex101.com, it only matches symbol_dead: "-". regex101.com/r/fsCfF7/1 Commented Feb 18, 2020 at 6:45
  • 1
    Back-references don't persist between different iterations of the regexp. Think of them as like local variables within a match, not global variables. Commented Feb 18, 2020 at 6:47
  • I have updated the regex! Commented Feb 18, 2020 at 8:26
  • 1
    @SasanAhmadi the syntax of that is wrong btw.. a backref leaves out the angle brackets (?P=somename). Anyway, you've got quite a few issues with that expression. If you look at the pastebin I linked, you've got nested groups with iterations on them. You've got to simplify your expression for it to work. When you use parenthesis, you tell the parser to group the results between them. If you want non-grouping parenthesis, you can use (?: ). Commented Feb 18, 2020 at 20:25
  • 1
    @SasanAhmadi here's some code using expressions to match your game board text: pastebin.com/4kf0kugr Commented Feb 18, 2020 at 23:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.