0

I am trying to learn some things about Regex. I am starting off by trying to hide some matches for a nine digit number, such as a SSN, but let through all nine digit numbers that have the word "order" or "routing number" but it seems that only strings that have the same length will work. Is there any way around this without creating multiple lines? Thanks!

(?<!(Order:\s|Routing\snumber:\s))
(?!000|666)([0-6]\d\d|7[01256]\d|73[0123]|77[012])
([-]?)
([1-9]{2})
\3
([1-9]{4})
(?!([\w&/%"-]))

For blocking out SSNs, this one seems to work ^(?!000)(?!666)(?!9)\d{3}([- ]?)(?!00)\d{2}\1(?!0000)\d{4}$ but I want it to not block out any 9 digit numbers that have the words "order" or "routing number" in front of them.

Alan
  • 53
  • 8
  • 1
    This regex is a mess. I also don't understand the problem. I would start off by simplifying the regex like this: http://pastebin.com/TuPB49uj – HamZa Jun 29 '15 at 15:29
  • 1
    This question would be easier to answer if you: (1) create a [minimal test regex](http://stackoverflow.com/help/mcve) that's much, much simpler than the one you currently have, (2) post some examples with that regex of what you want and don't want to match (along with the results you actually get), (3) post that regex with the examples on [an online regex tester](https://www.google.ch/search?q=regex%20tester) and include a link to that in the question (but the regex and examples should be in the question itself as well). – Bernhard Barker Jun 29 '15 at 15:31
  • 2
    @HamZa: are you sure that `[1-9]{2}` matches always the same thing than `(?!00)[0-9]{2}`? – Casimir et Hippolyte Jun 29 '15 at 15:41
  • I will update my question accordingly in a second. My main question was, I was told because "order" and "routing number" are not the same length of characters, it wont work. I was wondering if there was any way around that. – Alan Jun 29 '15 at 17:18
  • @CasimiretHippolyte I guess I'm getting rusty. My bad... – HamZa Jun 29 '15 at 20:40

1 Answers1

1

Many regular expression engines require lookbehind to be of fixed length, and will refuse to execute a variable-length lookbehind; if this is the case for yours, you should see a warning. If you're not seeing a warning, chances are the problem is that your regexp simply doesn't wok the way you think it does.

However, it is usually possible with lookbehinds to simply match the text you would prefer to count as a lookbehind, then discard/ignore it when you're inspecting the captures or match object.

user52889
  • 1,501
  • 8
  • 16