SoFunction
Updated on 2025-03-01

Example of using regular expressions for c# strings

1. Intercept the specified content in the string

Copy the codeThe code is as follows:

{"weatherinfo":{"city":"Beijing","cityid":"101010100","temp":"-4","WD":"Northwest wind","WS":"Level 2","SD":"29%","WSE":"2","time":"09:40","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}

Copy the codeThe code is as follows:

string pattern = "{\"weatherinfo\":(.*)}";
var result = (weatherQueryResult, pattern, ).Groups;

Copy the codeThe code is as follows:

The result is {"city":"Beijing","cityid":"101010100","temp":"-4","WD":"Northwest wind","WS":"Level 2","SD":"29%","WSE":"2","time":"09:40","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}

2. Intercept the numbers in the string

Copy the codeThe code is as follows:

string s = "B123-C12";
MatchCollection vMatchs = (s, @"(\d+)");
vMatchs[0].Value

Return results 123, 12

3. Intercept the letters in the string

Copy the codeThe code is as follows:

string str = "HaheheheaaB-cFe-sdfEww";
MatchCollection m = (str, @"[A-Z]+");//Lowercase letters are a-z and uppercase and lowercase and uppercase and lowercase and uppercase and zA-Z

Return result B/F/E