-1

I'm pretty new to regexp and I don't get why this expression is not matching the string

Regular expression: ^$0$.*|$5$(rounds=\d+$)?[a-zA-Z0-9./]{1,16}$[a-zA-Z0-9./]{43}$

String: $5$VcklQoY36plyldTg$cDc5UqmzE.NDnd/HbGXYzntHG9h4xycG2SSUSPH.Ww6

I see that $5$ in the target string, (rounds=\d+$) is optional. It matches the next 1-16 characters with the character set [a-zA-Z0-9./] and the next 43 characters with the character set [a-zA-Z0-9./]. How does the string not match the expression here?

I'm using the regcomp() and the regexec() functions from the linux regex library.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • You should also provide the code you use to run the regex. – Wiktor Stribiżew Mar 21 '23 at 22:20
  • Added it to the question now. – Siddharth Chandrasekar Mar 21 '23 at 22:23
  • 1
    Are you escaping your backslashes? Both C and your regex engine will have their own separate escapes, so `\d` should appear in C source as `\\d`, for example. – Dai Mar 21 '23 at 22:26
  • FWIW, Regex101.com also tells me your regex is invalid because your two `/` characters need to be escaped as well (assuming you're using a PCRE-style regex engine) – Dai Mar 21 '23 at 22:27
  • All but the last `$` should be `\$` (and thus `\\$` in a string literal) – ikegami Mar 21 '23 at 22:27
  • Assuming the string escaping is taken care of you'll still need to escape those `$` symbols so you can match literal $. `^\$0\$.*|\$5\$(rounds=\d+\$)?[a-zA-Z0-9./]{1,16}\$[a-zA-Z0-9./]{43}$` – motto Mar 21 '23 at 22:27

2 Answers2

1

$ symbol represents the end of a string in regex. If you want to match literal $ character it should be escaped.

Fixed regex: ^\$0\$.*|\$5\$(rounds=\d+\$)?[a-zA-Z0-9./]{1,16}\$[a-zA-Z0-9./]{43}$

Interactive example.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • @ikegami you are right, it depends on what delimiter the used language used. https://stackoverflow.com/questions/56101840/an-unescaped-delimiter-must-be-escaped-with-a-backslash – Vadim Martynov Mar 21 '23 at 22:33
0

$ has a special meaning in regex patterns. You need to escape it when you want to match a $ literally.

Also, you probably also meant to have a ^ in the right half of the alternation.

^\$0\$.*|^\$5\$(rounds=\d+\$)?[a-zA-Z0-9./]{1,16}\$[a-zA-Z0-9./]{43}$

or

^\$(0\$.*|5\$(rounds=\d+\$)?[a-zA-Z0-9./]{1,16}\$[a-zA-Z0-9./]{43})$

You didn't show your code, so you could also have made a mistake creating a C string literal that produces the above string. Keep in mind that \ is special in C string literals, so you need to escape it. (Same goes for ", but you're not using it.)

"^\\$(0\\$.*|5\\$(rounds=\\d+\\$)?[a-zA-Z0-9./]{1,16}\\$[a-zA-Z0-9./]{43})$"

This is what should appear in your code.

ikegami
  • 367,544
  • 15
  • 269
  • 518