*******高分求正则表达式

OceanRain 2009-05-06 04:24:46
用正则取两则之间的数字?(c#和javascript两种方式)
d0s0s3t1p0to99999i3m0to99999c50m1rp1.htm
如有一个函数 string[] test(string start, string end)
调用时:test("s","s") 返回0,3
test("p","to") 返回0,99999
test("m","to") 返回0,99999
...全文
107 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2009-05-06
  • 打赏
  • 举报
回复
C#中提供了方法可以对传入的变量做转义处理,以避免输入类似于“abc(def”这样的字符串时导致程序抛异常

private string[] test(string src, string start, string end)
{
Regex reg = new Regex(string.Format("{0}(\\d+){1}(\\d+)", Regex.Escape(start), Regex.Escape(end)));
Match m = reg.Match(src);
if (m.Success)
{
return new string[]{m.Groups[1].Value, m.Groups[2].Value};
}
else
return null;
}
//调用
string str = "d0s0s3t1p0to99999i3m0to99999c50m1rp1.htm";
string[] result = test(str, textBox1.Text, textBox2.Text);
if (result != null)
richTextBox2.Text += result[0] + "\n" + result[1];


javascript中MS就没有这样的函数了
<script type="text/javascript">
document.close();
document.open();

var str= "d0s0s3t1p0to99999i3m0to99999c50m1rp1.htm";
var result = test(str, "to", "i");
for(var i=1; i<result.length; i++)
{
document.write(result[i]);
document.write("<br>");
}

function test(src, start, end)
{
var re = new RegExp(start + "(\\d+)" + end + "(\\d+)", "i");
return src.match(re);
}
</script>
bloodish 2009-05-06
  • 打赏
  • 举报
回复

private void GetDigit(string input, string start, string end, out int startDigit, out int endDigit)
{
Regex reg = new Regex(string.Format("{0}(?<StartDigit>[\\d]+).*{1}(?<EndDigit>[\\d]+)",start,end));
Match m = reg.Match(input);
startDigit = endDigit = int.MinValue;
if(m.Success && m.Groups.Count >=3)
{
int.TryParse(m.Groups["StartDigit"].Value,out startDigit);
int.TryParse(m.Groups["EndDigit"].Value,out endDigit);
}
Console.WriteLine(string.Format("start digit:{0},end digit:{1}", startDigit, endDigit));
}


string str = "d0s0s3t1p0to99999i3m0to99999c50m1rp1.htm";
int startDigit,endDigit;

GetDigit(str,"d","s",out startDigit,out endDigit);
GetDigit(str,"p","to",out startDigit,out endDigit);
GetDigit(str,"c","m",out startDigit,out endDigit);

/*输出

start digit:0,end digit:3
start digit:0,end digit:99999
start digit:50,end digit:1

*/

LemIST 2009-05-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sq_zhuyi 的回复:]
string s = "d0s0s3t1p0to99999i3m0to99999c50m1rp1.htm";
public string[] GetString(string s1, string s2)
{
Regex reg = new Regex(String.Format("{0}([\d]+){1}([\d]+)", s1, s2));
Match mat = reg.Match(s);
if(mat.Success) return new string[]{ mat.Groups[1].Value, mat.Groups[2].Value };
else return null;
}
[/Quote]

S1, S2不能直接带入,有时需要转义,如.?{}[]等等。
OceanRain 2009-05-06
  • 打赏
  • 举报
回复
觉得这种方式用正则实现比较好
chinese_zmm 2009-05-06
  • 打赏
  • 举报
回复
楼上正解
路人乙e 2009-05-06
  • 打赏
  • 举报
回复
string s = "d0s0s3t1p0to99999i3m0to99999c50m1rp1.htm";
public string[] GetString(string s1, string s2)
{
Regex reg = new Regex(String.Format("{0}([\d]+){1}([\d]+)", s1, s2));
Match mat = reg.Match(s);
if(mat.Success) return new string[]{ mat.Groups[1].Value, mat.Groups[2].Value };
else return null;
}
HDNGO 2009-05-06
  • 打赏
  • 举报
回复
没看明白要干嘛
OceanRain 2009-05-06
  • 打赏
  • 举报
回复

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧