How to use word break, asterisk, word break in Regex with Perl?

perl, regex

Solution

It sounds like you want to treat `*` as a word character.

\b

is equivalent to

(?x: (?<!\w)(?=\w) | (?<=\w)(?!\w) )

so you want

(?x: (?<![\w*])(?=[\w*]) | (?<=[\w*])(?![\w*]) )

Applied, you get the following:

qr/
    (?: (?<![\w*])(?=[\w*]) | (?<=[\w*])(?![\w*]) )
    (FOO|BAR|\*)
    (?: (?<![\w*])(?=[\w*]) | (?<=[\w*])(?![\w*]) )
/x

But given our knowledge of the middle expression, that can be simplified to the following:

qr/(?<![\w*])(FOO|BAR|\*)(?![\w*])/

Problem

I have a complexe precompiled regular expression in Perl. For most cases the regex is fine and matches everything it should and nothing it shouldn't. Except one point. Basically my regex looks like: ``` my $regexp = qr/\b(FOO|BAR|\*)\b/; ``` Unfortunately `m/\b\*\b/` won't match `example, *`. Only `m/\*/` will do which I can't use because of false positives. Is there any workaround? from the comments - false positives are: `**`, `example*`, `exam*ple` what the regex is intended for? - It should extract keywords (one is a single asterisk) coworkers have entered into product data. the goal is to move this information out of a freetext field into an atomic one.

Original source