-2

currently I'm working on a Regex pattern, and when asserting "$" at the end of the line, it's causing issues for it not to grab anything. When I plug it into regex101, it shows that it should be working and grabbing my sample data, however, when testing it myself I've had zero luck. So I was just curious if there was an alternative solution for declaring the end of my regex pattern. My current example pattern is below.

^\W*((?i)Test(?-i))\W*\d{2}[\-]\d{2}[\-]\d{5}$

And using a regex tester shows that it should grab the value below.

Test 99-99-32123

However when attempting to test myself, when declaring $ at the end, it doesn't select any data. When I remove the $ though, then it grabs the above test data. I would still like to declare the end though to remove the chance of it grabbing false positives. So I was just curious if anyone sees an issue with what I'm doing, or has any alternative work arounds they could provide. Thanks!

Melon Man
  • 165
  • 1
  • 13
  • What flavor RegExp are you using? What language are you using to implement this pattern? The tokens you've elected to use have slightly different meanings across different RegExp flavors/language implementations. – esqew Mar 18 '22 at 15:08
  • Are there spaces at the end? `^\W*((?i)Test(?-i))\W*\d{2}[\-]\d{2}[\-]\d{5}\s*$` – The fourth bird Mar 18 '22 at 15:08
  • 2
    It is a common issue in .NET regex when you enable multiline option, you need to make sure you match an optional CR, so use `\r?$` instead of a `$`. `(?mi)^\W*Test\W*\d{2}-\d{2}-\d{5}\r?$` – Wiktor Stribiżew Mar 18 '22 at 15:12
  • No spaces at the end, I just double checked the file that I'm attempting to scan. Also I'm using terraform which is using the re2 library. If I were to remove the Test keyword, and just defined the pattern with ^ and $ at the end, I have no issues pulling the pattern. – Melon Man Mar 18 '22 at 15:13
  • @MelonMan Is it PCRE flavor then? Try `(*ANYCRLF)(?mi)^\W*Test\W*\d{2}-\d{2}-\d{5}$` – Wiktor Stribiżew Mar 18 '22 at 15:15
  • Wiktor's recommendation did the trick. Thank you so much, by declaring \r?$ at the end instead of $, it pulled the data. – Melon Man Mar 18 '22 at 15:16

1 Answers1

-2

Thanks to Wiktor's recommendation, changing the code to the below did the trick.

^\W*((?i)Test(?-i))\W*\d{2}[\-]\d{2}[\-]\d{5}\r?$

By using \r?$ instead of simply asserting $, it resolved the issue.

Melon Man
  • 165
  • 1
  • 13