user2796751
user2796751

Reputation: 13

Regex for integers 7-12 digits long

I have another question today. I have an email appliance that I am setting up to filter certain data, but it can only do this via regular expression. I have this partially accomplished thanks to this fine gentleman. What I need to accomplish now is something a bit more complex. I should add that I am a complete novice at regex. Right now i'm using this:

(?<!\d)(?!1000000)([1-7]\d{6}|8000000)(?!\d)

To find 7 digit integers within a range from 1000001 to 8000000, what i'm looking to do now is find integers between 1000000 and 12000000000, I can re purpose this code by simply changing up the section here

([1-7]\d{6}

But from my understanding this would require I format 5 separate expressions to find the data I need (I may be completely off base about this, but I don't know enough about regex to change this line to what I need). I would prefer one expression to look for data between 7-12 digits and stop at certain explicit 12 digit value. How can I accomplish this?

Upvotes: 0

Views: 1693

Answers (4)

Peter Alfvin
Peter Alfvin

Reputation: 29399

With the dust having settled, here is what I believe is the simplest "acceptable" range in question, with @hvd's caveats about use of \d:

\b([1-9]\d{6,9}|1[01]\d{9})\b

This includes 1000000 and excludes 12000000000 for the sake of simplicity.

Upvotes: 0

progrenhard
progrenhard

Reputation: 2363

^([1-9][0-9]{6,9}|1[0-2][0-9]{9})$

Regular expression visualization

Debuggex Demo

Upvotes: 0

user743382
user743382

Reputation:

1000000 to 12000000000 (exclusive) can be almost:

1000000 to 9999999: [1-9][0-9]{6}

10000000 to 99999999: [1-9][0-9]{7}

100000000 to 999999999: [1-9][0-9]{8}

1000000000 to 9999999999: [1-9][0-9]{9}

10000000000 to 11999999999: 1[01][0-9]{9}

Some regex syntax variants allow a{m,n} to get a anywhere from m to n times, allowing the first four of these to be combined to one. The full regex for a complete match would look like

[1-9][0-9]{6,9}|1[01][0-9]{9}

which you can then wrap in (?<![0-9])(...)(?![0-9]) to allow searching parts of strings.

This also matches 1000000, so to exclude that, you can use the same (?!...) construct you've already got, except modified to still allow 1000000 followed by other digits.

(?<![0-9])((?!1000000(?![0-9]))[1-9][0-9]{6,9}|1[01][0-9]{9})(?![0-9])

By the way, I'm using [0-9] instead of \d because I don't know which regex dialect you're using. \d also matches other digits than our 0123456789 in some dialects.

Upvotes: 1

Brian
Brian

Reputation: 7654

I might be misunderstanding your question, but isn't it just

^[1-9][0-9]{6,11}$

Upvotes: 0

Related Questions