Ralph Shillington
Ralph Shillington

Reputation: 21098

how do I capture a set of groups out of a string in particular order

I'm trying to build an alternative data entry, wherein the user will express some sort of command, that i'll parse. Rather than go into the details of the vocabulary I would be using in this effort here's an example of what I'm trying to accomplish with appoligies to Rex Harrison.

given the following sentences

the rain in spain falls on the plain

in spain on the plain falls the rain

on the meadow the snow falls in london

in pseudo regex:

(the (?<weather>\w+)) (in (<?city>\w+)) (falls) (on the (?<topography>\w+))

in short I need to harvest out of the sentence the weather, city and topography, using RegEx.

How do I express a set of captures that can occur in the input in any order?

Upvotes: 3

Views: 90

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

^(?:on the (?<area>\w+)() ?|the (?<weather>\w+)() ?|in (?<location>\w+)() ?|falls() ){4}\1\2\3\4$

will match a sentence that contains each of the elements exactly once in any order. That's what the empty parentheses are for - each one has to take part in the match so the final \1\2\3\4 can match.

The named backreferences will contain the variable elements.

Upvotes: 2

Max Shawabkeh
Max Shawabkeh

Reputation: 38603

First off, this looks like a problem that begs for a natural language parser.

But if you really want a regex solution, you'll have to pick out each pattern separately, either by using 3 regexes or by alternating them with pipes, e.g.:

(the (?<weather>\w+))|(in (<?city>\w+))|(on the (?<topography>\w+))

Running the above against any of your sample sentences, you'll get 3 matches, each of which will have one of its three groups set.

Upvotes: 2

Related Questions