Domanda

I have a following pattern rather complex:

^(?=.*\b(?:averages?|standard|means?)\b)(?=.*\b(?:goods?)\b)(?=.*\b(?:costs|cost to the company|sold by vendors?|bought from vandors?)\b).*$

and its very nicely matching the following sentences:

  1. What is average goods costs.

  2. give me a standard list of goods bought from vendors.

  3. list all standard goods sold by vendors.

I have to remove the matched pattern part from sentences i.e.

  1. what is __ list of _______.

  2. give me ________ for ______.

  3. list all _____________.

I am trying to split the pattern and thinking of performing match for every split instance of the pattern but its daunting so looking for a alternative solution thanks.

I trying to get the following to work.

string[] splitPat = value.Split(new string[] { ")(" }, StringSplitOptions.None);

THANKS

È stato utile?

Soluzione

Just put the pieces you want to be able to extract into groups by wrapping them in an extra set of (). For example:

^(?=.*\b((?:averages?|standard|means?))\b)(?=.*\b((?:goods?))\b)(?=.*\b((?:costs|cost to the company|sold by vendors?|bought from vandors?))\b).*$

When matching this string:

What is average goods costs.

average, goods and costs become the first, second and third group in your match.

Try playing with it here:

http://rubular.com/r/urb1raJ3W7

You can try different test strings and see what groups it will extract.

Then in .NET you can use Match.Groups to access the groups in a match. You can even name the groups if you want for easier maintanence.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top