0

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?

3
  • When you say "between 1000000 and 12000000000" did you mean to include the endpoints? Commented Sep 19, 2013 at 19:42
  • All i'm looking to do is start at 1000000 or 1000001 and end at 12000000000, if 1000000 matches, and/or if 12000000000 matches it doesn't matter, as the extra matches aren't going to throw off what i'm doing Commented Sep 19, 2013 at 21:53
  • Then you can drop that initial negative lookahead in your accepted answer. Commented Sep 19, 2013 at 21:57

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

14 Comments

Hat's off to you. I'm going to delete mine now. I learned something and you clearly have this well in hand! :-)
Could you simply this by adding a $ at the end of (?!1000000$)?
This would work, But i prefer the idea of modifying my original statement. I did try this approach and it works just fine however.
Heh, thanks :) The (?![0-9]) instead of $ is because the question asks for numbers as substrings in a larger string, to 1000000x should be rejected.
@user2796751 Ah, that's fair enough. I don't think the answer you accepted gives you the results you're asking for, though.
|
0

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.

5 Comments

This allows 00000000, does it not?
Updated and undeleted. :-)
Thank you, that's perfect. Removes some unnecessary complexity
If simplicity is the goal (and it should be), use word boundary (\b) instead of lookaround.
Indeed. Thanks for the feedback. Updated.
0

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

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

3 Comments

That includes numbers greater than 1.2*10**9
I think the number in the question was 1.2E10.
Whatever it is ;-) your regex still allows the first digit to 2 or more, which means it doesn't "stop" at 120000...
0
^([1-9][0-9]{6,9}|1[0-2][0-9]{9})$ 

Regular expression visualization

Debuggex Demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.