Regular Expression Errors A{1,2}==aaa?

liunx

Guest
Hi,<br /><br />I am trying to figure out why this piece of code does not work:<br /><br /><!--quoteo--><div class='quotetop'>QUOTE</div><div class='quotemain'><!--quotec-->$pattern = "a{1,3}";<br />$string = "aaaa";<br />if (!(eregi($pattern, $string))) {<br />die ("Error: incorrect verse format");<br />}<br /><br />//Returns true<!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />The curly brackets are letting me match the least amount, but the upper bound is not containing my regular expression.<br /><br />Thanks in advance!<br />Roy<br /> Thumbs Up<!--content-->
Regex expressions give me headaches... so I'll have to dust off my PHP Cookbook. I'll post what I find.<!--content-->
The best I've come up with so far indicates that you might want to try<br /><br />$pattern = "a{1,3}";<br />$string = "aaaaa";<br />if (!(eregi($pattern, $string)) || (strlen($string) > 3)) {<br />die ("Error: incorrect verse format");<br />}<br /><br />//Returns true<br /><br />I know I 'cheated' a bit... but I think it might accomplish what you're looking for. I couldn't find the answer on the eregi function that I was looking for.<br /><br />I'm not an expert of eregi and perl-like matching... but I believe the results you were getting came from the server thinking: look for any pattern that is the lowercase 'a' either once, twice, or three times in a row.<br /><br />I'm not sure, but playing around with it seems to indicate that the pattern we've used doesn't eliminate patterns of four or more consecutive characters.<br /><br />I'm sure someone could come up with the eregi expression... but I've got a killer headache working now.<!--content-->
Thanks Jack!<br /><br />I know what you mean about headache. The regular expression is the least of my programming for the day.<br /><br />I will go ahead and use the shortcut. Efficient regular expression programming can come another day.<br /><br />Roy<!--content-->
 
Back
Top