Rahul
Rahul

Reputation: 31

Regex to match a number pattern

How can I match a four digit number starting with 1 and which does not contain the number 1612 I tried

1[0-9]^6[0-9]^1[0-9]^2

but it doesn't work

Upvotes: 2

Views: 308

Answers (4)

prageeth
prageeth

Reputation: 7415

Try this code

\\b1(?!612)\\d{3}\\b

Upvotes: 1

Kippie
Kippie

Reputation: 3820

Does this work for you?

(?!1612)(1[0-9]{3})

Upvotes: 5

Pilou
Pilou

Reputation: 1478

You can try :

^1(?!692)[0-9]{3}$

Upvotes: 3

jlordo
jlordo

Reputation: 37843

String str = ...
if (str.matches("1[\\d]{3}") && !"1612".equals(str)) {
    // good to go
}

Upvotes: 0

Related Questions