int i = strSplit.IndexOf("<--"); int j = strSplit.IndexOf("-->",i); strText = strSplit.Substring(i+3,j-3) ;
用正则表达式string str=@"abc<-- efg --> hik<-- lmn <-- opq -->rst -->uvw ";Regex re = new Regex(@"\<\-\-\s*(?<data>.*?)\s*\-\-\>",RegexOptions.IgnoreCase|RegexOptions.Singleline); Match m = re.Match(str); if (m.Success) {
foreach(Match mcc in re.Matches(str)) { MessageBox.Show(mcc.Groups[data].value); }
为什么不自己编程解决呢? string str=" <-- jsoe slfeo wjid eij __ --> "; string temp; for(int i=0;i<str.Length;i++) { temp+=str[i]; if(IsTheKeyWordYouWantAnalyze(temp)) { ..... DoYourWork; temp=null; } else DoOtherThing; } in function IsTheKeyWordYouWantAnalyze,you could check that if temp is the keyword. what the most important is implement your own splitting syntax.
pattern改为: string pattern = @"\s*<--\s*|\s*-->\s*"; Regex r = new Regex(pattern); string[] result = r.Split(s);@"\s*<--\s*|\s*-->\s*"表示像: 空空<--空空,空空-->空空 或是没有空的字串 r.Split(s)表示用pattern来分割(就如string.Split一样)需要提取的字符在result中
比如:/<--(\.|\n)*-->$/ 匹配<--和-->之间的所有字符。
int i = strSplit.IndexOf("<--");
int j = strSplit.IndexOf("-->",i);
strText = strSplit.Substring(i+3,j-3) ;
efg
-->
hik<--
lmn
<--
opq
-->rst
-->uvw
";Regex re = new Regex(@"\<\-\-\s*(?<data>.*?)\s*\-\-\>",RegexOptions.IgnoreCase|RegexOptions.Singleline); Match m = re.Match(str);
if (m.Success)
{
foreach(Match mcc in re.Matches(str))
{
MessageBox.Show(mcc.Groups[data].value);
}
char[]charArray=new char[]{'<--'};
strArray=total.Split(charArray);
//第一次分割字符串char[]charArray=new char[]{'-->'};
strArray=total.Split(charArray);
//第二次分割字符串
" efg ad " +
" --> " +
"hik<-- " +
" lmn " +
" <-- " +
" opq " +
" -->rst " +
"-->uvw";
string pattern = @"\s*<?-->?\s*";
Regex r = new Regex(pattern);
string[] result = r.Split(s);
foreach(string re in result)
Console.WriteLine(re);
}
string str=" <-- jsoe slfeo wjid eij __ --> ";
string temp;
for(int i=0;i<str.Length;i++)
{
temp+=str[i];
if(IsTheKeyWordYouWantAnalyze(temp))
{ .....
DoYourWork;
temp=null;
}
else
DoOtherThing;
}
in function IsTheKeyWordYouWantAnalyze,you could check that if temp is the keyword. what the most important is implement your own splitting syntax.
string pattern = @"\s*<--\s*|\s*-->\s*";
Regex r = new Regex(pattern);
string[] result = r.Split(s);@"\s*<--\s*|\s*-->\s*"表示像: 空空<--空空,空空-->空空 或是没有空的字串
r.Split(s)表示用pattern来分割(就如string.Split一样)需要提取的字符在result中
字符串是可以解析出来了,但是我想把解析出来的字符串用另外的字符串替换掉,这又如何实现呢?
比如我想把解析出来的“efg”这个字符串替换成“GetName()”,正则表达式可以实现吗?
" efg " +
" --> " +
"hik<-- " +
" lmn " +
" <-- " +
" opq " +
" -->rst " +
"-->uvw";s = Regex.Replace(s,"efg","GetName()");