Regular Expression to matching against multiple ranges of different lengths

regex number range 1-1000
regex tester
regex match specific number
regex multiple character ranges
regex match number
javascript regex
regex any number of digits
regex limit number of digits

I need to validate if the value a user enters is one of the following:

  • a 7 digit number ranging from 7000000 - 7999999
  • a 9 digit number ranging from 777000000 - 777777777
  • an 11 digit number ranging from 77700000000 - 77777777777

What I have so far and that seems to work is: ^7\d{7}|777\d{7}|777\d{9}$

However, since I'm new at regex, I wanted to confirm if this is the most efficient way.

Thanks in advance!

Example: Matching Numeric Ranges with a Regular Expression, Detailed example of building a regex to match a number in a given range of numbers. Though a valid regex, it matches something entirely different. [0-255] is� To match any number from 1 to 9, regular expression is simple /[1-9]/ Similarly you may use /[3-7]/ to match any number from 3 to 7 or /[2-5]/ to match 2,3,4,5. Regex for 0 to 10. To match numbers from 0 to 10 is the start of a little complication, not that much, but a different approach is used. This series is broken in to two components. 1

This is not a problem for regexes. Regexes are for matching patterns, not evaluating numeric values.

Get the user input and then compare it to the numerical values.

$ok =
    ($n >= 7000000 && $n <= 7999999) 
    ($n >= 777000000 && $n <= 777777777)
    ($n >= 77700000000 && $n <= 77777777777);

Regexes are not a magic wand you wave at every problem that happens to involve strings.

Finally, don't worry about "most efficient" until you have "works correctly."

Sets and ranges [], However, using regular expressions to check text length can be useful in some situations, particularly when length is only one of multiple rules that determine whether the any minimum or maximum text length, or allow characters other than A–Z. will fail to match if the subject text's length falls outside the desired range. A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that define a search pattern.Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.

Your best bet is to capture the full digit string with ^(7\d{6}|777\d{6}|777\d{8})$ and then evaluate it as a number (this assumes that the 7, 9 or 11 digit number is the entire input).

4.9. Limit the Length of Text, Library Carpentry: Introduction to Working with Data (Regular Expressions) A string could be any length, ranging from empty (zero characters) to one that spans Match on types of characters (e.g. 'upper case letters', 'digits', 'spaces', etc.). Square brackets can be used to define a list or range of characters to be found. A string is said to match a regular expression if it is a member of the regular set described by the regular expression. As with LIKE , pattern characters match string characters exactly unless they are special characters in the regular expression language — but regular expressions use different special characters than LIKE does.

Given that floating numbers and conditionals don't mix very well, here is a regex that should cover all the target ranges :



      7 \d{6} 

           777 [0-6] \d{7} 
        |  7777 [0-6] \d{6} 
           (?: 77 )?
                777 [0-6] \d{5} 
             |  7777 [0-6] \d{4} 
             |  77777 [0-6] \d{3} 
             |  777777 [0-6] \d{2} 
             |  7777777 [0-6] \d 
             |  77777777 [0-7] 

Regular Expressions – Library Carpentry: Introduction to Working , Matching Floating Point Numbers with a Regular Expression . can think of regular expressions as wildcards on steroids. or the letter X. Again, the order of the characters and the ranges does not for the character position, it would return the length of the string if string Other scripts like Latin span multiple languages. 4.9. Limit the Length of Text Problem You want to test whether a string is composed of between 1 and 10 letters from A to Z. Solution All the programming … - Selection from Regular Expressions Cookbook, 2nd Edition [Book]

Wouldn't this work?


[PDF] Regular Expressions: The Complete Tutorial, what they can't do. On an abstract level a regular expression, regex for short… Other engines often provide an equivalent escape syntax. In Go, you would use This pattern matches all 100 two digit strings in the range from 00 to 99 . You can define multiple ranges within the same character class. A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. “find and replace”-like operations.(Wikipedia). Regular expressions are a generalized way to match patterns with sequences of characters.

Everything you need to know about Regular Expressions, The text is based on the ed description and substantially modified, primarily to aid subexpression within the same larger RE may match different lengths even in similar Common practice is for a character class expression to match a collating In the POSIX locale, a regular expression that starts with a range expression� Here&#39;s an interesting regex problem: I seem to have stumbled upon a puzzle that evidently is not new, but for which no (simple) solution has yet been found. I am trying to find a way to exclude an entire word from a regular expression search. The regular expression

Regular Expressions, A regular expression, specified as a string, must first be compiled into an This method compiles an expression and matches an input sequence against it in a Instances of this class are immutable and are safe for use by multiple 3, Range, a-z Note that a different set of metacharacters are in effect inside a character� Multiple character ranges can also be used in the same set of brackets, along with individual characters. An example of this is the alphanumeric \w metacharacter which is equivalent to the character range [A-Za-z0-9_] and often used to match characters in English text.

Pattern (Java Platform SE 7 ), There are three separate approaches to pattern matching provided by PostgreSQL: returns true or false depending on whether its pattern matches the given string. specifies a character class, just as in POSIX regular expressions. the regexp split functions ignore zero-length matches that occur at the start or end of the� This was the first part of my introduction to Regular Expression in R. For more information detailed information about all input parameters of each function, please consult the base R manual . In subsequent parts, I will introduce you to so-called Anchors, Character Classes, Groups, Ranges, and Quantifiers.

  • Can the numbers also be e.g. 777099999? or must they only contain the digits 0 to 7?
  • Why don't just test if the number is in the range?
  • Efficient == shortest? For 9- and 11-digit numbers there are as many conditions as the digits that can change. It would be much easier to parse the numbers, otherwise it's a bit code-golf question.
  • ^7\d{7} matches an 8 digit number. Did you mean ^7\d{6} ?
  • Won't this disallow 777000009?