alan turing
alan turing

Reputation: 463

Regex Match if there is space in front

I need a regex which matches with 1 to 6 length digits which is easy \d{1,6}, however I need avoid if there is longer number than 6 digits.

For example, it should match 233 in sentence, my id is 233, but it should not match my id is 222334444

It should only match if there is space before first digit of number.

Thanks

Upvotes: 1

Views: 1990

Answers (3)

user557597
user557597

Reputation:

Its hard to tell what the expected character is on either side.

Generally, it could be done using word boundary \b\d{1,6}\b

Upvotes: 1

Dalorzo
Dalorzo

Reputation: 20014

How about?

Online Demo

(?<!\d)\d{1,6}$

Upvotes: 0

Aran-Fey
Aran-Fey

Reputation: 43166

Use lookarounds.

(?<=^|\s)\d{1,6}(?!\d)

Upvotes: 0

Related Questions