Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex X/Y coordinate

I have data that fits this pattern: (x|y). x and y can be negative, and in this case the sign - is present. I'm trying to build a regex expression to match the x and y.

Here is my current expression, which seems valid to me but is not working:

/\((-?\d+)\|(-?\d+)\)/

Here is the raw data :

‭(-112|‭‭-522‬‬)
‭(-112|‭‭522‬‬)
(112|-‭‭522‬‬)
(112|‭‭522‬‬)

Any ideas?

like image 555
BadTigrou Avatar asked Nov 09 '22 06:11

BadTigrou


1 Answers

You have some invisible Unicode characters in your test data.

Remove them, and your regex will work just fine.

For instance, your example of (-112|‭‭-522‬‬) is actually\u0028\u002d\u0031\u0031\u0032\u007c\u202d\u202d\u002d\u0035\u0032\u0032\u202c\u202c\u0029.

You have a few U+202D (LEFT-TO-RIGHT OVERRIDE) and U+202C (POP DIRECTIONAL FORMATTING) in there.

If you want to allow these in your regex, you could include them:

\(\p{Cf}*(-?\p{Cf}*\d+)\p{Cf}*\|\p{Cf}*(-?\p{Cf}*\d+)\p{Cf}*\)

But the pattern gets pretty messy. I just added a bunch of \p{Cf}* in there to allow these characters. Note thet you'll still have to get rid of the characters between the minus sign and the digits before you attempt to convert the captured substrings to integers.

It would probably be much simpler to just replace everything that matches \p{Cf}+ with an empty string before proceeding further with your original pattern.

like image 195
Lucas Trzesniewski Avatar answered Nov 14 '22 23:11

Lucas Trzesniewski