Regular expression to create a line of a cock game (tic-tac-toe)

Asked

Viewed 127 times

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 find basically this Pattern: | | O | X |, that is I want in the middle of the Pipes | | may exist only on the left side: espaço, in the middle: 0 or X or espaço, right: espaço. I’d like you to return to me None if the Pattern is not exactly like this, but 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 | | |, have | X |, works the same!

What’s the matter?

2 answers

3

Recalling that the | is a special character in regular forms and represents the or to capture you literally you must use \|

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

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

basically what she does is seek

| + connector + |

I am using the "o" and the "x" lowercase, I don’t know if Python holds i at the end similar to javascript /\|\s+(o|x| )\s+\|/i to search for capital and minuscule at the same time, however in case it is necessary to change to \|\s+(o|O|x|X| )\s+\|.

  • 1

    Test site http://regex101.com/r/bL5mO0/1

0

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

house with:

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

but does not marry:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.