regexpcre

Basic PCRE expression to match word anywhere in a line + another word always at the end of line


Sorry, I had a problem formulating the subject as precisely as I wanted. But I wonder if you could help me with a basic PCRE regular expression that will match/capture like this: If line contains the word "foo", then "foo" should be captured anywhere on the line. If the line contains "foo", anywhere on the line, and the word "bar", "bar" should always be captured but only if it is at the end of the line. If "bar" is the only word in the line, it should be captured. So,

"foo" -> capture "foo"
"bar foo" -> capture "foo"
"foo bar" -> always capture "bar" last, "foo" may also be captured
"bar" -> capture "bar"
"abc foo" -> capture "foo"
"abc foo bar" -> capture "bar". "foo" may also be captured
"abc bar" -> should give "no match"

I am trying the following:

~/ % pcretest
PCRE version 8.45 2021-06-15

  re> "(?:.*?(foo)?).*?(bar)?$"
data> foo
 0: foo
 1: foo
data> foo bar
 0: foo bar
 1: foo
 2: bar
data> bar
 0: bar
 1: <unset>
 2: bar
data> bar foo
 0: bar foo

It goes wrong in the line, "bar foo", "foo" should be captured but isn't.


Solution

  • May be this can help: /^(bar)$|^.*foo.*(bar)$|^.*(foo)/mg