-6

The string should not contain SSN or phone number. The regex below does not work, it accepts only xxxxxxxxx format.

Should not contain xxx-xx-xxxx or xxx-xxx-xxx or xxxxxxxxx.

regex = "^((?!\\d[9]$)|(?!(\\d{3}-?\\d{2}-?\\d{4}$)|(?!(\\d{3}-?\\d{3}-?\\d{3})$)$";

user679526
  • 845
  • 4
  • 16
  • 39
  • 6
    "does not work" is not helpful; please give specific examples. – Oliver Charlesworth Dec 31 '12 at 16:25
  • Your regex looks very complicated. I'm not sure I understand what you're looking for, but from what I can gather, does this work: `^\d{3}-?\d{2}-?\d-?\d{3}$` – bozdoz Dec 31 '12 at 16:30
  • The string should not contain either xxx-xxx-xxx or xxx-xx-xxxx or xxxxxxxxx (9 digits) numbers. What is the ? for – user679526 Dec 31 '12 at 16:32
  • This will also knock out US drivers licenses for the states of Colorado, Connecticut, Mississippi, and New Mexico. Will this be a problem? – Clockwork-Muse Dec 31 '12 at 16:34
  • @user679526 What string? What are you doing with it? – bozdoz Dec 31 '12 at 16:36
  • No, it would not be a problem. I tried the above regular expression, it is still allowing 9 digit number and also xxx-xx-xxxx format numbers. – user679526 Dec 31 '12 at 16:37
  • Could you show us what you are trying the regex on? – bozdoz Dec 31 '12 at 16:37
  • using in a hibernate valiadtor, @Pattern(regexp = "^(?!\\d{3}-?\\d{2}-?\\d-?\\d{3})$", message = "error") – user679526 Dec 31 '12 at 16:38
  • I'm not sure how it works in java, but I'm familiar with `?!` being "not followed by". Your example doesn't have anything before it; I think that's the error. – bozdoz Dec 31 '12 at 16:41
  • @user679526 What exactly you are looking for? Are you looking for all types of `phones number` and `SSN` from given text? Please be specific. – Smit Dec 31 '12 at 17:15

1 Answers1

4

You might try:

^(?!((\\d{9})|(\\d{3}-\\d{2}-\\d{4})|(\\d{3}-\\d{3}-\\d{3}))$).*

To explain, if we read the query you provided:

^((?!\\d[9]$)|(?!(\\d{3}-?\\d{2}-?\\d{4}$)|(?!(\\d{3}-?\\d{3}-?\\d{3})$)$

We could read that: is not followed by xxxxxxxxx OR is not followed by xxx-xx-xxxx OR is not followed by xxx-xxx-xxx (in my version at the top, I rephrased this to be: is not (xxxxxxxxx OR xxx-xx-xxxx OR xxx-xxx-xxx).).

Any string in the world is guaranteed to not match at least two of those, so the combination of them is always true, leaving you with a much simpler effective regex:

^$

?! is a zero-width assertion, so it consumes nothing. Even when you match what's checked by the lookaheads, you aren't consuming the input, and so you never reach $, that's required outside the lookaheads. Simply adding a .* before the final $ fixes that.

Your hyphens shouldn't be followed by ?, I don't think. Making them optional, means you also match xxx-xxxxxx and xxx-xx-x-xxx. If that's intended you can add them back, or simplify your regex considerably, to:

^(?!\\d{3}-?\\d{2}-?\\d-?\\d{3}$).*

The other problem is \\d[9]$ should be \\d{9}$

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • aa 123-123-124 is not a valid string, but the above regex is allowing it. – user679526 Dec 31 '12 at 19:20
  • 2
    That's right. If you want to prevent any string being matched containing such a pattern that occurs anywhere within the string, `^(?!.*(\\d{9})|(\\d{3}-\\d{2}-\\d{4})|(\\d{3}-\\d{3}-\\d{3})).*` would do it. – femtoRgon Dec 31 '12 at 19:30
  • I changed the regex to ^(?!.*(\\d{16})|.*(\\d{9})|.*(\\d{3}-\\d{2}-\\d{4})|.*(\\d{3}-\\d{3}-\\d{3})).* to validate "text 123123123" and also "123123123 text" – user679526 Dec 31 '12 at 20:32