I'm trying to extract a portion of the string,I use two patterns, the string I'm interested in is in the middle of these two pattern...But I get this error ... where did I fail?
[System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length] at System.String.Substring(Int32 startIndex, Int32 length) at Program.Main() :line 16
This is the code
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
string pattern = @"<Code>";
string _pattern = @"</Code>";
Regex regex = new Regex(pattern);
Regex _regex = new Regex(_pattern);
string str = "<?xml version=\"1.0\" encoding=\"UTF-16\" ?><Codici QuoteParams><Code>AC-ALTRE-SPESE-T</Code></Codici QuoteParams>";
var matches = regex.Matches(str);
var _matches = _regex.Matches(str);
string firstString = str.Substring(0+str.IndexOf(matches[0].ToString()),str.IndexOf(_matches[0].ToString()));
Console.WriteLine(firstString);
}
}