0

I'm trying to write a regular expression to capture a single word that appears on either side of a pattern. Here are a couple of examples of possible strings with the value I need extracted into a single named group regardless of their position, in all caps:

  • ... INFO [example] ...
  • ... SEVERE [example] ...
  • ... [example] ERROR ...
  • ... [example] WARN ...

It seemed like a simple problem to solve at first but I still haven't been able to find a solution. Any help would be appreciated.

Thanks.

P.Ackland
  • 53
  • 4

1 Answers1

1

What language/implementation/runtime are you using?

PCRE enables multiple named capture groups with the (?J) mode modifier, but in many implementations (Perl included), you can't meaningfully use duplicate capture groups unless they are part of separate alternatives of the regex.

i.e. in Perl and PHP: The match for the following will be unusable when trying to match "... ERROR":

/(?J)(?<type>INFO)?.*?(?<type>ERROR)?/

That's because the first capture group "wins" and is empty. (See below)

However...

/(?J)(?<type>INFO|SEVERE)\b.*|.*\b(?<type>ERROR|WARN)/ will work

I wasn't entirely clear on what you were trying to match, so I'm not sure if the above regular expressions are in any way representative of what you're doing

More on duplicate named groups

From https://www.regular-expressions.info/named.html

... In Perl, a backreference matches the text captured by the leftmost group in the regex with that name that matched something...

So in Perl and Ruby, you can only meaningfully use groups with the same name if they are in separate alternatives in the regex, so that only one of the groups with that name could ever capture any text. Then backreferences to that group sensibly match the text captured by the group.

Community
  • 1
  • 1
Watson
  • 129
  • 4
  • `/(?J)(?INFO|SEVERE)\b.*|.*\b(?ERROR|WARN)/` turned out to be exactly what I need. Fortunately, I am using PCRE and I can manually identify which cases appear on either side, so this solves my problem. Thanks! – P.Ackland Oct 31 '19 at 23:44