来个正则高手

验证码识别 2011-03-15 12:30:35
input:
----------------------
u="3" ach="67" classId="5"
u="4" ach="251" classId="0"
-------------------------

我要得到如下结果
-----------------------------
be:67/5
be:251/0

用下面形式的代码
Regex re = new Regex(pattern);
MatchCollection myMatchs = re.Matches(input);


能否实现?
正则表达式pattern怎么写?
...全文
206 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2011-03-15
  • 打赏
  • 举报
回复
可以
string result = Regex.Replace(yourStr,@"(?m:^).+?\bach=\x22(\d+).+classId=\x22(\d+)\S+","be:$1/$2");
验证码识别 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyazhe 的回复:]
C# code
string test = @"u=""3"" ach=""67"" classId=""5"";
foreach(Match m in Regex.Matches(test,@"(?m:^).+?ach=\x22(\d+).+?classId=\x22(\d+)"))
{
string result = "be:" + m.Groups[1].Value + "/……
[/Quote]

没有办法在正则表达式中直接替换为 be: ?
h2ero 2011-03-15
  • 打赏
  • 举报
回复
我写的烂代码,

string str = "u=\"3\" ach=\"67\" classId=\"5\" u=\"4\" ach=\"251\" classId=\"0\"";
Regex re = new Regex("(.*?)ach=\"(.*?)\"(.*?)classId=\"(.*?)\"(.*?)");
MatchCollection result = re.Matches(str);
foreach (Match item in result)
{

Console.WriteLine("be:"+item.Groups[2]+"/"+item.Groups[4]);
}
Console.ReadLine();
h2ero 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 yalan 的回复:]

引用 5 楼 wuyazhe 的回复:
C# code
string test = @"u=""3"" ach=""67"" classId=""5""";
foreach(Match m in Regex.Matches(test,@"(?m:^).+?ach=\x22(\d+).+?classId=\x22(\d+)"))
{
string result = "be:" + m.G……
[/Quote]
确实好!
yalan 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wuyazhe 的回复:]
C# code
string test = @"u=""3"" ach=""67"" classId=""5""";
foreach(Match m in Regex.Matches(test,@"(?m:^).+?ach=\x22(\d+).+?classId=\x22(\d+)"))
{
string result = "be:" + m.Groups[1].Value + "……
[/Quote]

恩恩,这个好~~~~~~
Joop_Song 2011-03-15
  • 打赏
  • 举报
回复
楼上的几位都是大牛啊!领教了,mark、、
兔子-顾问 2011-03-15
  • 打赏
  • 举报
回复
string test = @"u=""3"" ach=""67"" classId=""5""";
foreach(Match m in Regex.Matches(test,@"(?m:^).+?ach=\x22(\d+).+?classId=\x22(\d+)"))
{
string result = "be:" + m.Groups[1].Value + "/" + m.Groups[2].Value + "\n";
Console.WriteLine(result);
}
兔子-顾问 2011-03-15
  • 打赏
  • 举报
回复
string test = @"u=""3"" ach=""67"" classId=""5"";
foreach(Match m in Regex.Matches(test,@"(?m:^).+?ach=\x22(\d+).+?classId=\x22(\d+)"))
{
string result = "be:" + m.Groups[1].Value + "/" + m.Groups[2].Value + "\n";
Console.WriteLine(result);
}
-过客- 2011-03-15
  • 打赏
  • 举报
回复
try...

string test = @"u=""3"" ach=""67"" classId=""5""  
u=""4"" ach=""251"" classId=""0""";
Regex reg = new Regex(@"(?i)ach=""([^""]*)""\s+classid=""([^""]*)""");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += "be:" + m.Groups[1].Value + "/" + m.Groups[2].Value + "\n";
}
xiaoqiu1234 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhoukang0916 的回复:]
C# code

//写得太烂了
string inputStr = "u=\"3\" ach=\"67\" classId=\"5\" u=\"4\" ach=\"251\" classId=\"0\"";

Regex re = new Regex("(?i).*?ach=\"(?<ach>.*?)\"\\sclassId=\"(?<classId>.*?)……
[/Quote]
验证码识别 2011-03-15
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wuyazhe 的回复:]
可以

C# code
string result = Regex.Replace(yourStr,@"(?m:^).+?\bach=\x22(\d+).+classId=\x22(\d+)\S+","be:$1/$2");
[/Quote]

用这个
Regex re = new Regex(pattern);
re.Match(input);

有没有可能匹配的结果 match.value 就是替换后的?
有这样的pattern没有?
PandaIT 2011-03-15
  • 打赏
  • 举报
回复

//写得太烂了
string inputStr = "u=\"3\" ach=\"67\" classId=\"5\" u=\"4\" ach=\"251\" classId=\"0\"";

Regex re = new Regex("(?i).*?ach=\"(?<ach>.*?)\"\\sclassId=\"(?<classId>.*?)\"");
MatchCollection myMatchs = re.Matches(inputStr);
string str = "";
foreach (Match item in myMatchs)
{
str += "de:" + item.Groups["ach"].Value + "/" + item.Groups["classId"].Value;
}
MessageBox.Show(str);

111,096

社区成员

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

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

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