0

The following:

Regex.Matches("Some Text @param Some More", "(.*)(@param)(.*)")

returns

  • Some Text @param Some More
  • Some Text
  • @param
  • Some More

Is there any way to not have the first line? I cannot find any documentation on this. And, if I use online parsers, they only list the 3 groups...I would like to avoid having to code in the ignore first...

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180

2 Answers2

2

Use a named group captures to not have to work with indexing. Change to this:

Regex.Matches("Some Text @param Some More", "(?<One>.*)(@param)(?<Two>.*)")

then access the match data such as

var data1 = mt.Groups["One"].Value;
var data2 = mt.Groups["Two"].Value;
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Choosing as the answer as this is the best to answer my question directly. I ended up going with the skip option once I used OfType, though – Justin Pihony Jul 29 '13 at 19:47
  • @JustinPihony index 0 is always the full match regardless of submatches. Then in order of match each index thereafter is each of the submatches. That is true on every match. By using named match captures one doesn't have to figure out which sub match is which. – ΩmegaMan Jul 29 '13 at 20:13
1

Try this:

Regex.Matches("Some Text @param Some More", "^.*(?=@param)|@param|(?<=@param).*");
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125