Izanatos
Izanatos

Reputation: 33

RegEX : Parsing text for exact string match or same string with underscore at the end

I am trying to parse a text file in PHP and group the content based on string match.

Text sample:

xxxx xxxx xx xxxx xxx foo xxx xxx xxxxx

xxxx xx foo xxx xxx xxxxx

xx xxxxx xxxx xx xxx xx bar xx xxxx

xxxxxx xx xxxxx x xxxx bar

xxx xxxxx xx xx foo_sub1 xxx xxxx

xx foo_sub2 xxxxx xxx x xxxx

xxx xx foo1_sub1 xxx xxxx

xxxxx foo1_sub2 xxxxx xxx x xxxx

private function stringContains ($string, $substring){
    if (preg_match("~\b$substring\b~", $string)) {
        return true;
    }
}

the problem is that if the substring is foo it will not return me foo_sub, any idea how to edit my regex to include any foo and foo_ but not foo1 or foo1_?

Thanks,

Upvotes: 2

Views: 211

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626816

You may use

\bfoo(?:_\w+)?\b

See the regex demo

Details

  • \b - word boundary
  • foo - some fixed value
  • (?:_\w+)? - an optional part:
    • _ a _
    • \w+ - one or more letters, digits or _ chars
  • \b - word boundary

See PHP demo:

$str = 'xxxx xxxx xx xxxx xxx foo xxx xxx xxxxx\nxxxx xx foo xxx xxx xxxxx\nxx xxxxx xxxx xx xxx xx bar xx xxxx\nxxxxxx xx xxxxx x xxxx bar\nxxx xxxxx xx xx foo_sub1 xxx xxxx\nxx foo_sub2 xxxxx xxx x xxxx\nxxx xx foo1_sub1 xxx xxxx\nxxxxx foo1_sub2 xxxxx xxx x xxxx';
if (preg_match_all('~\bfoo(?:_\w+)?\b~', $str, $matches)) {
    print_r($matches[0]);
}

Output

Array
(
    [0] => foo
    [1] => foo
    [2] => foo_sub1
    [3] => foo_sub2
)

Upvotes: 1

Related Questions