tags:

views:

72

answers:

3

Hello,

This regex only allows alphanumeric characters and one period. I haven't be able to figure out how to set a quantifier to limit the number of characters to say between five and twenty. I'm using it in PHP.

/^([a-zA-Z0-9\.](?!\.)|[a-zA-Z0-9]*\.(?!\.))[a-zA-Z0-9]*$/
+3  A: 

Use the {n,m} quantifier for at least n and at most m repetitions.

Gumbo
That's the obvious answser, but trying to apply it to it's case is the tricky part.
e-satis
Yes I'm having a hard time applying the quantifier.
lanmind
@lanmind: What exactly do you want to express?
Gumbo
@Gumbo I would like the regex to match alphanumeric characters and one period. The period can be anywhere in the string but only one period is allowed.
lanmind
@ Gumbo, I'm sorry I meant:I would like the regex to match a string of alphanumeric characters and one period. The period can be anywhere in the string but only one period is allowed. The string must be between five and twenty five characters.
lanmind
As you’re using PCRE, you can use a look-ahead assertion: `/^(?=[^.]*\.[^.]*$)[a-zA-Z0-9.]{5,25}$/`.
Gumbo
@Gumbo: It seems to work. I'm going to explain the regex how I understand it in another answer below.
lanmind
A: 

Use

/^[a-zA-Z0-9]{5, 20}$/
A: 
/^ ---------> search starting at the beginning for
( ----------> begin group that consists of
?= ---------> looking ahead for
[^.]* ------> no periods any number of times
\.? --------> followed by a period optionally (?) then
[^.]* ------> no periods any number of times
$ ----------> search starting at the end of the string for contents of the group
) ----------> end group
[a-z0-9.] --> a letter number or period
{5,25} -----> five to twenty five times
$ ----------> search starting at the end of the string for the whole previous pattern
/i ---------> case insensitive

I added the question mark after the period inside the look ahead to make it optional. Am I correct in understanding the regex as I did above? Specifically too, am I correct in that the dollar sign at the end of the look ahead searches for the look ahead starting at the end of the string? I've never seen a dollar sign anywhere but at the very end of a regex. Thank you for the help.

lanmind
The lookahead *starts* matching at the beginning of the string (because it's right after the `^` anchor); the `$` ensures that it matches all the way to the *end* of the string, the same as it always. But once the lookahead is done, the match position returns to the beginning of the string so the next part of the regex can start matching there. Lookaheads aside, you're correct that the dollar sign usually isn't useful anywhere but the end of the regex.
Alan Moore
By the way, this is the first time you've mentioned that the period was optional. Are there any other requirements you haven't mentioned? For example, if the period is present, is it allowed to appear as the first or last character in the string?
Alan Moore
Thank you Alan. The way you explained it helped me understand regexs even better. Sometimes explanations are constructed well and it is to the benefit of the lesser experienced.
lanmind
I realized I didn't mention the period being optional but I figured adding the question mark would make it so. Yes, the period is allowed to appear at the beginning or end. This regex matches that in testing I've done. Do you use an online regex tool Alan? I've seen a few but I'm not sure if they are functioning correctly. I admit though I haven't given them much time. I mostly test in with my scripts.
lanmind
A look-aead assertion is like your reading a book and you want to see what’s on the next few paragraphs/pages. You’re bookmarking your current position, read ahead and then return to your bookmarked position. In the case, where I placed the `$` inside the look-ahead, it means that you just want to know when the boring book you have to read for homework finally ends.
Gumbo
That's a funny and easy way to remember it! Thanks.
lanmind
I use online tools sometimes to test regexes before I post them--is that what you mean? For example, I use this one to test PHP regexes: http://www.solmetra.com/scripts/regex/ (you want to choose one that matches the flavor you're working with). If you're talking about *learning* regexes, that's down to experience, forums like this, and The Book: http://www.amazon.com/exec/obidos/ASIN/0596528124/masteringregu-20
Alan Moore
Thanks a bunch Alan.
lanmind