Home > Mobile >  Regular expression for extracting a noun in variable order
Regular expression for extracting a noun in variable order

Time:02-05

I have the following text:

Action by Toni Kroos, Real Madrid. Rayo Vallecano 2, Real Madrid 0.

where the nouns are Toni Kroos, Real Madrid (team1) and Rayo Vallecano (team2).

I need a regular expression with a named capturing group that returns these results given the following variations:

Action by Toni Kroos, Real Madrid. Rayo Vallecano 2, Real Madrid 0.

Expected result: Rayo Vallecano

Action by Toni Kroos, Real Madrid. Real Madrid 0, Rayo Vallecano 2.

Expected result: Rayo Vallecano

My naive intention was to negate the backreference captured in team1 and use it on the second sentence. So when it is about to match Real Madrid or Rayo Vallecano, it would discard Real Madrid as is the same value as team1. So team2 would return Rayo Vallecano. No lucky so far with something like (it only works on the first example):

^Action by .*\, (?<team1>.*)\. (?!\1)(?<team2>.*)( \d \,| \d \.).

I'm no expert with regular expressions, so I'm not sure that's possible to achieve with one unique pattern that fits both examples. Is it possible to get such expression? If so, I would appreciate the solution with an explanation of the pattern used. Thanks in advance.

EDIT: The language I'll be using is JavaScript

CodePudding user response:

Regular expression answer

You can use the following regular expression with named capturing groups:

Action by.*, (?<team1>.*)\. (?<team2>.*) (\d ), (?<team1>.*) (\d )\.

This regex matches the text and captures the values of team1 and team2 using named capturing groups. Note that the named capturing group team1 is used twice in the expression to capture both values of the same team.

Check the link of the picture. I hope this helps.

CodePudding user response:

Take a look at this: (https://regex101.com/r/yYgl5R/1)

Regex: ^Action by .*\, (.*)\. (?<team1>.*)( \d \,) (?<team2>.*)(\d \.)

This matches only the teams, and the score

  • Related