Osvaldas
Osvaldas

Reputation: 13

PCRE whole word match (only spaces allowed as separator)

I want to match string as a whole word match "~\b$search\b~i", it matches '35' to '35-40'. I want only space to be separator between words.

Test cases: Matching 35 in following cases:

Thanks for answers

Upvotes: 1

Views: 171

Answers (1)

Gumbo
Gumbo

Reputation: 655239

You can use look-around assertions instead of word boundary assertions:

~(?<!\S)$search(?!\S)~i

Here (?<!\S) asserts that there is no non-whitespace character (\S) preceding and (?!\S) asserts that there is no non-whitespace character following $search.

Upvotes: 1

Related Questions