Re: Regex Length: {3} *OR* {6}?
Posted: Tue May 13, 2008 4:47 pm
Not quite sure whether you want to match an empty string or not and what you mean by those multiple :'s.*non-EDIT Edit* Forums weren't responsive for about an hour and a friend suggested the following...This won't match empty strings <input value="" /> being empty. I can also get :: or ::: or ::::: to match.Code: Select all
/[0-9a-z]((.|,|:)[0-9a-z]){0,10}/
Also, as I mentioned in my previous reply, you realise that you are NOT matching "a dot OR a comma OR a colon" with the regex "(.|,|:)" ? That part of your regex will match any character because of the dot.
The regex (.|,|:) can be read as: "any character OR a comma OR a colon".
Oh, that is of no surprise to me. I merely pointed out the probable errors in your regex.It's closer then where I was earlier though.
Ok on to my hour ago reply...
Thanks for your suggestion though none of the valid CSS selectors validate with your suggested regex filter.
Ok, matching those strings above can be done using the following regex:I have posted a fully functional regex filter tester with text inputs with existing CSS selectors in posts.
But if you're just better with a list here are some examples of valid CSS selectors (keeping in mind I am not going to need to filter {} curely brackets).
Code: Select all
body h1 h1, h2, h3, h4 div div.border a:hover a:link a:focus
Code: Select all
'/^[a-z0-9]+([.,:](?!$))?(\s*[a-z0-9]+\1?)*?$/'Code: Select all
#!/usr/bin/php
<?php
$tests = array("body", "h1", "h1, h2, h3, h4", "div", "div.border", "a:hover", "a:link", "a:focus", // your correct values
"bOdy", "h!", "h1, h2. h3: h4", "div.", "?div.border", "a::hover"); // some wrong values?
$regex = '/^[a-z0-9]+([.,:](?!$))?(\s*[a-z0-9]+\1?)*?$/';
foreach($tests as $t) {
if(preg_match($regex, $t)) {
print "Yes, a match : $t\n";
} else {
print "No match : $t\n";
}
}
/* output:
Yes, a match : body
Yes, a match : h1
Yes, a match : h1, h2, h3, h4
Yes, a match : div
Yes, a match : div.border
Yes, a match : a:hover
Yes, a match : a:link
Yes, a match : a:focus
No match : bOdy
No match : h!
No match : h1, h2. h3: h4
No match : div.
No match : ?div.border
*/
?>So I made a more or less educated guess as to what you don't want to match. Extending the above regex to accept empty strings should be trivial.