Jump to content

Recommended Posts

Posted

Hi,

 

I am trying to figure out why this piece of code does not work:

 

$pattern = "a{1,3}";

$string = "aaaa";

if (!(eregi($pattern, $string))) {

die ("Error: incorrect verse format");

}

 

//Returns true

 

The curly brackets are letting me match the least amount, but the upper bound is not containing my regular expression.

 

Thanks in advance!

Roy

Thumbs Up

Posted (edited)

The best I've come up with so far indicates that you might want to try

 

$pattern = "a{1,3}";

$string = "aaaaa";

if (!(eregi($pattern, $string)) || (strlen($string) > 3)) {

die ("Error: incorrect verse format");

}

 

//Returns true

 

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.

 

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.

 

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.

 

I'm sure someone could come up with the eregi expression... but I've got a killer headache working now.

Edited by surefire
Posted

Thanks Jack!

 

I know what you mean about headache. The regular expression is the least of my programming for the day.

 

I will go ahead and use the shortcut. Efficient regular expression programming can come another day.

 

Roy

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...