Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match to string length by using regex in python

Writing python regex for string. I want the string to be at least 1 symbol and max 30. The problem is that im using 3 sub-blocks in regex letters, so there always must be 3 characters long length. Is it possible to add that condition in this regex (1-30 characters length):

regex = re.compile("^[a-zA-Z]+[a-zA-Z0-9\.\-]+[a-zA-Z0-9]$")
r = regex.search(login)

Thank you.

like image 307
dima.h Avatar asked Aug 15 '13 13:08

dima.h


People also ask

How do you find the length of a string in regex Python?

To check the length of a string, a simple approach is to test against a regular expression that starts at the very beginning with a ^ and includes every character until the end by finishing with a $.

How do you mention length in a regular expression?

Simple, complete and tested java code, for finding words of certain length n: int n = 10; String regex = "\\b\\w{" + n + "}\\b"; String str = "Hello, this is a test 1234567890"; ArrayList<String> words = new ArrayList<>(); final Pattern pattern = Pattern. compile(regex, Pattern.

How do you restrict length in regex?

The ‹ ^ › and ‹ $ › anchors ensure that the regex matches the entire subject string; otherwise, it could match 10 characters within longer text. The ‹ [A-Z] › character class matches any single uppercase character from A to Z, and the interval quantifier ‹ {1,10} › repeats the character class from 1 to 10 times.

Is regex a match in Python?

match() function of re in Python will search the regular expression pattern and return the first occurrence. The Python RegEx Match method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object.


2 Answers

Although it is not clear which 1 or 2 length character strings you want to accept I propose the following regex:

regex = re.compile("^[a-zA-Z][a-zA-Z0-9\.\-]{0,28}[a-zA-Z0-9]$")

As the middle set includes all other this will directly match all words with length 3-30 as you wish.

I hope this regex also matches your 2 length strings (I just assumed that the first character must be a letter), you need to add something (using '|') for single letter matches.

like image 82
eci Avatar answered Oct 26 '22 22:10

eci


In general, this is difficult and doing some work outside of the RE (as suggested in the comment by M. Buettner) is often required. Your problem is easier because it can be reduced to a pattern with only one repeating element.

You have one or more letters, followed by one or more of (letter, digit, dot, hyphen) followed by a single (letter or digit), right? If so, the repetition of the first group is not needed. Leave off the + to get

r"^[a-zA-Z][a-zA-Z0-9\.\-]+[a-zA-Z0-9]$"

and you will match exactly the same set of strings. Any extra leading letters past the first will be matched in the second group instead of the first.

Now, the only variable portion of your RE is the middle section. To limit the overall length to 30, all you need do is limit that middle portion to 28 characters. Change the + to {1,28} to get:

r"^[a-zA-Z][a-zA-Z0-9\.\-]{1,28}[a-zA-Z0-9]$"

You can read more about Python REs at:

http://docs.python.org/2/library/re.html

like image 34
Mike Housky Avatar answered Oct 26 '22 23:10

Mike Housky