I have any text in C#, and I need “match” using Regular Expressions, and get a value (parsing the text for get the value).
Texts:
var asunto1 = “ID P20101125_0003 —
Pendiente de autorización –“;var asunto2 = “ID P20101125_0003 any
text any text”;var asunto3 = “ID_P20101125_0003 any
text any text”;
I need get the value:
var peticion = “P20101125_0003”;
I have this regular expression, but fails for me:
//ID P20101125_0003 -- Pendiente de autorización --
patternPeticionEV.Append(@"^");
patternPeticionEV.Append(@"ID P");
patternPeticionEV.Append(@"(20[0-9][0-9])"); // yyyy
patternPeticionEV.Append(@"(0[1-9]|1[012])"); // MM
patternPeticionEV.Append(@"(0[1-9]|[12][0-9]|3[01])"); // dd
patternPeticionEV.Append(@"(_)");
patternPeticionEV.Append(@"\d{4}");
//patternPeticionEV.Append(@"*");
patternPeticionEV.Append(@"$");
if (System.Text.RegularExpressions.Regex.IsMatch(asuntoPeticionEV, exprRegular, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
var match = System.Text.RegularExpressions.Regex.Match(asuntoPeticionEV, exprRegular, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
//...
}
Your regular expression ends with “$” which says “the line/text has to end there”. You don’t want that. Just get rid of this line:
and it will mostly work immediately. You then just need to add a capturing group to isolate the bit of text that you want.
I’d also recommend adding
using System.Text.RegularExpressions;so that you don’t have to fully qualifyRegexeach time. You can also callMatchand then check for success, to avoid matching it twice.Sample code: