Regex Not Capturing Alphanumeric Sequences with Special Characters in PHP - guide with Word Boundaries
I'm not sure how to approach I'm trying to capture sequences of alphanumeric characters that may also contain special characters like underscores and hyphens in a string using regex in PHP. However, I'm struggling with ensuring that I properly capture words at the boundaries and avoid partial matches. Here's what I have so far: ```php $pattern = '/\b[\w-]+\b/'; $string = 'This is a test_string-1 with some words, such as example_word and test-case.'; $matches = []; preg_match_all($pattern, $string, $matches); print_r($matches); ``` I expected to get all the words including those with underscores and hyphens, but the output is quite irregular: ``` Array ( [0] => Array ( [0] => This [1] => is [2] => a [3] => test_string [4] => 1 [5] => with [6] => some [7] => words [8] => such [9] => as [10] => example_word [11] => and [12] => test [13] => case ) ) ``` The hyphenated 'test-case' is split into 'test' and 'case', which is not what I want. I also tried using the pattern `'/\b[\w-_]+\b/'`, but that didn't work as expected either. I'm currently using PHP 8.1, and I'm aware that regex can be tricky with boundaries. Is there a more efficient way to handle this, or am I missing something in my regex pattern? Any help would be greatly appreciated! Any ideas what could be causing this?