Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

Regular expression - PCRE (PHP) - word boundary (\b) and accent characters

Hey everyone,

I'm currently working on some PHP code that involves regular expressions, specifically using PCRE (Perl Compatible Regular Expressions) in PHP. I have a question regarding the `\b` word boundary token and its behavior with accent characters.

In my code, I need to match specific words and ensure that they are not part of longer words. Ideally, the `\b` word boundary token should match the start or end of a word, which is perfect for my needs. However, I've noticed that when it comes to accent characters, things may not work as expected.

For example, let's say I want to match the word "café" using the regex pattern `\bcafé\b`. I would expect this to match the word "café" surrounded by spaces or punctuation marks. However, it seems that the presence of the accent character is causing the word boundary to fail and not match properly.

I've tried adjusting my pattern by using Unicode character classes like `\p{L}` to match any letter, including accent characters. But even then, the problem remains.

I'm wondering if anyone has faced a similar issue or has any insight into how to properly handle accent characters with the `\b` word boundary token in PCRE (PHP). Is there a workaround or a different approach I should consider?
Any help or suggestions would be greatly appreciated. Thanks in advance!

All Replies


Hey folks,

I stumbled upon this thread and thought I'd share a different perspective on the issue with accent characters and the `\b` word boundary in PCRE (PHP).

In my experience, I faced a similar challenge when working on a project that involved matching words with accent characters, particularly in longer texts. I wanted to ensure accurate boundary matching for words like "hôtel" and "déjà vu."

What worked for me was using a combination of the `\b` word boundary token and a modified Unicode character class. Instead of solely relying on `\b`, I incorporated the `\b(?=\w)(?<=\w)` pattern. This approach helped me perform word boundary matching while taking into account the presence of accent characters.

By using `\b(?=\w)(?<=\w)` before and after the accented word, I was able to correctly match and capture these words within the text, regardless of the context or surrounding characters. It proved effective in scenarios where I needed to isolate specific words with accent characters precisely.

I hope this alternative approach proves helpful to those dealing with similar challenges. Feel free to give it a try or share your own solutions if you've come across any!

Happy coding, everyone!


Hey there!

I've encountered a similar issue before when working with accent characters and the `\b` word boundary in PCRE (PHP). What I found is that the behavior of `\b` can indeed be affected by accent characters.

In my case, I wanted to match the word "élite" using the pattern `\bélite\b`. However, I faced the same problem you described - the word boundary failed to match the word properly whenever the accent character was present.

After some research and experimentation, I discovered that the issue arises because the `\b` word boundary token considers accent characters as part of the word itself. Therefore, it treats "élite" as a single word without any boundaries.

To work around this, I modified my regex pattern to use lookaheads and lookbehinds instead. For example, I used `(?<!\w)élite(?!\w)` to ensure a proper word boundary for "élite". This approach helped me match the word correctly, regardless of the presence of accent characters.

Please give it a try and let me know if it works for you. If anyone else has further insights or alternative solutions, I'd love to hear them too!

New to LearnPHP.org Community?

Join the community