Search code examples
phpregexword-boundary

word boundaries preg_replace


I would like to catch any piece of string that matches %[a-z0-9], respecting the following examples :

1. %xxxxxxxxxxxxx                                 //match
2. this will work %xxxxxx but not this%xxxxxxxxx. //match 1st, not 2nd
3. and also %xxxxxxxxxx.                          //match
4. just a line ending with %xxxxxxxxxxx           //match
5. %Xxxxxxxxxxx                                   //no match
6. 100% of dogs                                   //no match
7. 65%. Begining of new phrase                    //no match
8. 65%.Begining of new phrase                     //no match

It can be at the begining of the string or at the end, but not in the middle of a word. It can of course be in the string as a word (separated by space).

I have tried

/(\b)%[a-z0-9]+(\b)/
/(^|\b)%[a-z0-9]+($|\b)/
/(\w)%[a-z0-9]+(\w)/

and others like this, but I can't get it to work like I would. I guess the \b token does not work in example 2 because there is a boundary before the % sign.

Any help would be greatly appreciated.


Solution

  • Try

    /\B%[a-z0-9]+\b/
    

    You don't have a word boundary \b between a space and the %, but you have one between s and %.

    \B is the opposite of \b not a word boundary.

    See it here on regex101