Home > Net >  C # a regular expression
C # a regular expression

Time:11-01

String STR="zhang SAN" ${} ABC, dick, and harry.
Strings str1="${def} 001";
Want to obtained through regular expressions to STR character of ABC and str1 character of def

CodePudding user response:

https://www.cnblogs.com/fdyang/p/6842547.html
You can consult

CodePudding user response:

 string STR="zhang SAN" ${} ABC, dick, and harry. 
The Regex r=new Regex (" {} [\ \ s \ \ s] * ");
MatchCollection MC=r.M atches (STR);
for (int i=0; i {
String res=MC [I] Value;
}

CodePudding user response:

Without regular expression line not?
 
Class Program
{
The static void Main (string [] args)
{
Var STR="zhang SAN" ${} ABC, dick, and harry.

Var indexOfLeft=STR. IndexOf (" ${") + 2;
Var indexOfRight=STR. IndexOf ("} ");
Var result=STR. The Substring (indexOfLeft indexOfRight - indexOfLeft);

Console. WriteLine (result);
}
}

CodePudding user response:

Don't use regular simple "split" mechanism,
 var STR="zhang SAN" ${} ABC, dick, and harry; 
Var I=STR. Split (new string [] {" ${}, StringSplitOptions. None);
If (i.L ength & gt;
=2){
Var j=I [1]. The Split ("} ");
If (j.L ength & gt;
=2){
The Debug. Assert (j [0]=="ABC");
}
}


Or if you are sure there must be the target in the STR separator, you can save the if judgment:
 var result=STR. Split (new string [] {" ${}, StringSplitOptions. None) [1]. The Split ('} ') [0]; 
The Debug. Assert (result=="ABC");

CodePudding user response:

Consult, thank you for sharing
  •  Tags:  
  • C#
  • Related