Regular expression to create a line of a rooster game (old game)

2

I'm trying to use a pattern to find a certain line in a file, but it's not working.

This is my pattern: row = re.compile(r"(|\s[OX\s]\s{3}|)")

With this I want to basically find this pattern: | | O | X | , that is, I want that in the middle of the | | pipes can exist only on the left side: espaço , in the middle: 0 or X or espaço , right: espaço . I would like to return None if the pattern is not exactly like that, but it is not working.

row = re.compile(r"(|\s[OX\s]\s{3}|)")
exp = re.match(row, line) 

If line is different, for example instead of | X | | | , I have | X | , it works the same!

What's the problem?

    
asked by anonymous 24.11.2014 / 16:33

2 answers

4

Remembering that | is a special character in regular expressions and represents or to literally capture it you should use \|

With this expression I believe you can find what you want.

\|\s+(o|x| )\s+\|

Basically what it does is search

+ + + + I'm using " o " and " x " lowercase, I do not know if Python behaves at the end like javascript /\|\s+(o|x| )\s+\|/i to search for uppercase and lowercase at the same time, however, if necessary change to \|\s+(o|O|x|X| )\s+\| .

    
24.11.2014 / 16:55
1

tries to use this way \|\s\|([OX\s]\|x\S) .

Home with:

| |O|x| , | |X|X| .

but not home to:

|X| | | , |O| | | .

    
24.11.2014 / 16:57