In C #, use the formal expression to match and obtain the required data,
In. NET, use the formal expression to match and obtain the required data.
Requirement: obtain the required data for regular expression matching in a string.
For example, the following string:
String temp = "ErrorCode:-1, Message: {" UserId ":" 1000 "," userName ":" ZhangSan "}";
I need to get "-1" and "{" UserId ":" 1000 "," userName ":" ZhangSan "}";
Next, use the regular expression to match:
Using System. text. regularExpressions; string temp = "ErrorCode:-1, Message: {\" UserId \ ": \" 1000 \ ", \" userName \ ": \" ZhangSan \"}"; regex reg = new Regex ("ErrorCode :(? <Key1> .*?), Message :{(? <Key2> .*?)} "); Match match = reg. match (temp); string tempStr = match. groups ["key1"]. value + "--" + match. groups ["key2"]. value; MessageBox. show (tempStr );
In this case, tempStr obtains "-1 -- {" UserId ":" 1000 "," userName ":" ZhangSan "}"