Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match specific length x or y

Tags:

regex

I'd like a regex that is either X or Y characters long. For example, match a string that is either 8 or 11 characters long. I have currently implemented this like so: ^([0-9]{8}|[0-9]{11})$.

I could also implement it as: ^[0-9]{8}([0-9]{3})?$

My question is: Can I have this regex without duplicating the [0-9] part (which is more complex than this simple \d example)?

like image 782
Laoujin Avatar asked Oct 08 '12 15:10

Laoujin


People also ask

What is RE I in python?

A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

Is regex matching fast?

Regular expression matching can be simple and fast, using finite automata-based techniques that have been known for decades. In contrast, Perl, PCRE, Python, Ruby, Java, and many other languages have regular expression implementations based on recursive backtracking that are simple but can be excruciatingly slow.

What is literal matching?

A literal character or matching something literally refers to specifying an actual character in the text: for instance, a to match a , as opposed to a character class such as \w that could also match a .


1 Answers

There is one way:

^(?=[0-9]*$)(?:.{8}|.{11})$ 

or alternatively, if you want to do the length check first,

^(?=(?:.{8}|.{11})$)[0-9]*$ 

That way, you have the complicated part only once and a generic . for the length check.

Explanation:

^       # Start of string (?=     # Assert that the following regex can be matched here:  [0-9]* # any number of digits (and nothing but digits)  $      # until end of string )       # (End of lookahead) (?:     # Match either  .{8}   # 8 characters |       # or  .{11}  # 11 characters )       # (End of alternation) $       # End of string 
like image 121
Tim Pietzcker Avatar answered Sep 17 '22 14:09

Tim Pietzcker