jcbjoe
jcbjoe

Reputation: 51

Regex - Escaping square brackets along with boundry

I have a website where users can have custom actions when a keyword is detected in a sentence. How I currently do matches is like the following:

$output = array();
preg_match('/\b' . $keyword . '\b/', $phrase, $output);

If I find a match if(count($output) > 0) { then the custom action is ran. This is for spoken sentences so it is for things like operator, we have a custom one called [silence] so when silence is detected it runs an action.

However when the keyword contains brackets for example: [silence] the regex fails because it has square brackets. I have tried escaping both like \b\[silence\]\b However this does not detect a match.

Also this is in PHP

Thanks in advance, Joe

Upvotes: 1

Views: 30

Answers (1)

Joffrey Schmitz
Joffrey Schmitz

Reputation: 2438

The "word boundary" expression matches if the next character is a part of a word, and [ isn't (it is not a letter)

From Regex tutorial :

There are three different positions that qualify as word boundaries:

  • Before the first character in the string, if the first character is a word character.
  • After the last character in the string, if the last character is a word character.
  • Between two characters in the string, where one is a word character and the other is not a word character.

Simply put: \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b. A “word character” is a character that can be used to form words. All characters that are not “word characters” are “non-word characters”.

So you need to "rewrite" the \b expression that can suit your need, like :

(?<=[\s\.,;])\[silence\](?=[\s\.,;])

First, a non-matching "delimiter character" (space, dot, comma, ... You probably need to add a few more), followed by your expression, followed by a non-matching delimiter character again.

Upvotes: 2

Related Questions