extracting a substring from a huge string
I have a huge string. I need to extract a substring from that that huge string. The conditions are the string starts with either "TECHNICAL" or "JUSTIFY" and ends with a number, any number from 1 to 10. so for example, i have
string x = "This is a test, again I am test TECHNICAL: I need to extract this substring starting with testing. 8. This is test again and again开发者_JS百科 and again and again";
so I need this
TECHNICAL: I need to extract this substring starting with testing.
I was wondering if someone has elegant solution for that.
Thanks in advance.
You can use Regular Expression for that.
Example:
string input = "This is a test, again I am test TECHNICAL: I need to extract this substring starting with testing. 8. This is test again and again and again and again";
string pattern = @"(TECHNICAL|JUSTIFY).*?(10|[1-9])";
System.Text.RegularExpressions.Regex myTextRegex = new Regex(pattern);
Match match = myTextRegex.Match(input );
string matched = null;
if (match.Groups.Count > 0)
{
matched = match.Groups[0].Value;
}
//Result: matched = TECHNICAL: I need to extract this substring starting with testing. 8
精彩评论