5

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?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
BadTigrou
  • 111
  • 10

3 Answers3

4

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.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
0

You can test this in Regexr, which is where I came up with it, but this should do what you're looking for

Regex code

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

Tested values

(-112|-522)
(-112|522)
(112.35|-522)
(112|522.5)
Pazuzu156
  • 679
  • 6
  • 13
  • Ya know, I didn't think of that, I'll fix it. As for the g, I stated I used regexer, so I just copied it's output from the share option. I'll fix that too – Pazuzu156 Aug 19 '15 at 21:55
  • Answer has been edited to remove the g flag and fixed multiple decimal issue. – Pazuzu156 Aug 19 '15 at 22:12
-3

You have to escape the minus sign with a backslash like this \-. It's reserved for patterns like a-z

Karl
  • 833
  • 6
  • 13