Exclude underscore from word boundary in regex
Asked Answered
B

3

7

I'm using regex word boundary \b, and I'm trying to match foo in the following $sentence but the result is not what I need, the underscore is killing me, I want underscore to be word boundary just like hyphen or space:

$sentence = "foo_foo_foo foo-foo_foo";
              X   X   X  YES  X   X

Expected:

$sentence = "foo_foo_foo foo-foo_foo";
             YES YES YES YES YES YES

My code:

preg_match("/\bfoo\b/i", $sentence);
Blount answered 16/3, 2015 at 2:15 Comment(5)
Have you tried searching online?Interstate
i cannot find this specific question on googleBlount
Are you able to use preg_split here instead of preg_match?Ratfink
@Blount There are articles online about this and the related questions section has many different questions relevant to his.Interstate
@Ratfink it must be preg_match :(Blount
S
12

You would have to create DIY boundaries.

(?:\b|_\K)foo(?=\b|_)
Stereophotography answered 16/3, 2015 at 2:32 Comment(0)
S
0

You can subtract _ from the \w and use unambiguous word boundaries:

/(?<![^\W_])foo(?![^\W_])/i

See this regex demo. Note \bfoo = (?<!\w)foo and foo(?!\w) = foo\b, and subtracting a _ from \w (that is equal to [^\W]) results in [^\W_].

In PHP, you can use preg_match_all to find all occurrences:

preg_match_all("/(?<![^\W_])foo(?![^\W_])/i", $sentence)

To replace / remove all occurrences, you may use preg_replace:

preg_replace("/(?<![^\W_])foo(?![^\W_])/i", "YES", $sentence)

See the PHP demo online:

$sentence = "foo_foo_foo foo-foo_foo";
if (preg_match_all("/(?<![^\W_])foo(?![^\W_])/i", $sentence, $matches)) {
    print_r($matches[0]);
}
// => Array( [0] => foo [1] => foo [2] => foo [3] => foo [4] => foo [5] => foo)
echo PHP_EOL . preg_replace("/(?<![^\W_])foo(?![^\W_])/i", "YES", $sentence);
// => YES_YES_YES YES-YES_YES
Sebrinasebum answered 30/5, 2021 at 20:59 Comment(0)
R
-1

Does this do what you want?:

preg_match_all("/foo/i", $sentence, $matches);
var_dump($matches);
Resolvent answered 16/3, 2015 at 2:28 Comment(1)
no, because it will also match foobar and i only want full wordsBlount

© 2022 - 2024 — McMap. All rights reserved.