求正则表达式~
^abc^def^ghi^jkl^mnopqrst^
给定了以上字符串,如何把两个 "^ "中的内容用正则表达式解析出来?
结果应该为
abc
def
ghi
jkl
mnopqrst
谢谢~~!!
------解决方案--------------------这样 不需要 正则吧
string a = "^abc^def^ghi^jkl^mnopqrst^ ";
string[] myArr = a.Split( '^ ');
------解决方案--------------------正则表达式可以用这个 @ "\^([^\^]+)\^ ";
------解决方案--------------------正则表达式可以用(? <=\^).*?(?=\^)
就你这一问题,可以这样做
string yourStr = "^abc^def^ghi^jkl^mnopqrst^ ";
string resultStr = Regex.Replace(yourStr, @ "(? <=\^)(.*?)(?=\^) ", "$1\n ").Replace( "^ ", " ");
或者
string yourStr = "^abc^def^ghi^jkl^mnopqrst^ ";
MatchCollection mc = Regex.Matches(yourStr, @ "(? <=\^)(.*?)(?=\^) ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox1.Text += m.Groups[1].Value + "\n ";
}
------解决方案--------------------@ "\^([^\^]+)\^ " 会漏掉
楼上的正确
foreach (Match m in Regex.Matches(yourStr, @ "(? <=\^)(.*?)(?=\^) "))
richTextBox1.AppendText(m.Result( "$1 ") + "\r\n ");