5

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.

6
  • Why not check it separately in code? Also, it would be helpful if we had some valid and invalid 1- and 2-character example input. Commented Aug 15, 2013 at 13:26
  • What kind of input of length 1 or 2 would be allowed to match? What should not match? What if the length goes to 3 or up? Commented Aug 15, 2013 at 13:27
  • Some sample data please, or at least what are you trying to match ? Commented Aug 15, 2013 at 13:27
  • Just for example add this regexp from question to online regexp testing tool and write "ab". I mean i want to check for match (>1<30) this 2 characters string only using regulars (not using if ...) ? Commented Aug 15, 2013 at 13:37
  • Would a- be valid? Would - be valid? What about 0? You need to think your question through more. Commented Aug 15, 2013 at 13:39

2 Answers 2

8

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.

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

1 Comment

Alkmost the same. Now everybody knows it takes me at least 15 min. to type one of these answers. Sheesh. You do want to change {0,28} to {1,28} if you want to match the + instead of * in the original RE, though...so I guess I did add a microsopic bit.
1

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

1 Comment

2 characters length string not matching (example "ab")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.