Regex X / Y coordinate

I have data that matches this pattern: (x|y) . x and y can be negative, in which case the - sign - present. I am trying to build a regex expression according to x and y.

Here is my current expression, which seems valid to me, but doesn't work:

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

Here is the source data:

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

Any ideas?

+5
source share
3 answers

Your test data has invisible Unicode characters.

Delete them and your regex will work just fine .

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

You have several U+202D (LEFT-TO-RIGHT OVERRIDE) and U+202C (POP DIRECTIONAL FORMATTING).

If you want to allow them in your regular expression, you can include them:

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

But the template gets pretty dirty. I just added a bunch of \p{Cf}* to allow these characters. Note that you still have to get rid of the characters between the minus sign and the numbers before you try to convert the captured substrings to integers.

It would probably be a lot easier to just replace anything that matches \p{Cf}+ with an empty string before continuing with your original template.

+4
source

You can test this in regexr where I came up with this, but that should do what you are looking for

Regex code

 /\((-?[0-9]+\.?[0-9]+)\|(-?[0-9]+\.?[0-9]+)\)/ 

Validated Values

 (-112|-522) (-112|522) (112.35|-522) (112|522.5) 
0
source

You need to avoid the minus sign with a backslash like \- . It is reserved for patterns like az

-3
source

All Articles