Confused about matches

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jon23d
Forum Newbie
Posts: 10
Joined: Wed Jun 07, 2006 3:47 am

Confused about matches

Post by jon23d »

I have a string in which I need to replace several tokens.

The tokens are {{small-images}}, {{medium-images}}, {{images}}, {{xl-images}}, and {{xxl-images}}

I want to take the first one found, and then replace all the others with that.

Then I want to replace all the tokens with a given string.

But I can't seem to get past the first part...

As a test, I'm running:

Code: Select all

 
$pattern = "/{{([a-zA-Z]-)?images}}/";
$string = "this is a test {{small-images}}.  Yep{{medium-images}} and {{images}} and {{xl-images}} and {{xxl-images}}.";
 
if (preg_match($pattern, $string, $matches)) {
    echo preg_match($pattern, $string) . " matches found:<br />";
    print_r($matches);
}
 
For whatever reason, only 1 match is being found, and it is just {{images}}, not {{small-images}}...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Confused about matches

Post by pickle »

You're only matching one occurrence of a-zA-Z, so the rest of the word isn't found.

This pattern works:

Code: Select all

/{{[a-zA-Z]*-images}}/
If you know exactly what the strings are, use str_replace() - it's much more efficient.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jon23d
Forum Newbie
Posts: 10
Joined: Wed Jun 07, 2006 3:47 am

Re: Confused about matches

Post by jon23d »

I can use str_replace after determining what the first token is, so I think I have to use regex first.

I made a slight change to yours, and now it works perfectly (though I still only get 1 match, but I guess that doesn't matter):

Code: Select all

/{{[a-zA-Z-]*images}}/
Thank you!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Confused about matches

Post by pickle »

If you want multiple matches, use preg_match_all()

Maybe I'm not understanding your problem 100%. Why you you need to determine what the first token is? Even then - strpos() 6 times is probably more efficient.

Regular expressions have tons of overhead & should only be used when you don't know exact content of strings.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Confused about matches

Post by prometheuzz »

jon23d wrote:...

For whatever reason, only 1 match is being found, and it is just {{images}}, not {{small-images}}...
This is what your regex really means:

Code: Select all

{{            // match '{{'
(             // open group 1
  [a-zA-Z]    //   match a single character in the range a-z or A-Z
  -           //   match a hyphen
)             // close group 1
?             // group 1 is optional
images        // match 'images'
}}            // match '}}'
So, as you can see, the substring "{{images}}" only matches it.

You probably meant to do something like this:

Code: Select all

$pattern = "/{{(?:[a-zA-Z]*-)?images}}/";
$string = "this is a test {{small-images}}.  Yep{{medium-images}} and {{images}} and {{xl-images}} and {{xxl-images}}.";
 
if (preg_match_all($pattern, $string, $matches)) {
    echo preg_match($pattern, $string) . " matches found:<br />";
    print_r($matches);
}
And replacing all the '{{ ... }}' with the first occurrence of '{{ ... }}' ('{{small-images}}' in this case), can be done like this:

Code: Select all

$pattern = "/{{(?:[a-zA-Z]*-)?images}}/";
$string = "this is a test {{small-images}}.  Yep{{medium-images}} and {{images}} and {{xl-images}} and {{xxl-images}}.";
 
if (preg_match($pattern, $string, $matches)) {
    echo preg_replace($pattern, $matches[0], $string);
}
Post Reply