正则表达式

zhan1219 2009-11-28 07:18:29
在C#中如何用正则提取"@RA@YA@L2@Q2@EA"中的@前面第一个值和第二个值,如
提取后变成RYLQE和AA22A
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yankunlin 2009-11-28
  • 打赏
  • 举报
回复
怎样学习正则表达式????????????????
会的人加我qq 975292958
zpx3243 2009-11-28
  • 打赏
  • 举报
回复
Dim html As String = "@RA@YA@L2@Q2@EA"

Dim reg As Regex = New Regex("@(?<first>.{1})(?<second>.{1})")
Dim s1 As String = reg.Replace(html,"${first}")
Dim s2 As String = reg.Replace(html,"${second}")

<first>.{1}:红色表示匹配字符的个数,可以根据需要修改。
wuyi8808 2009-11-28
  • 打赏
  • 举报
回复
using System;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
string s = "@RA@YA@L2@Q2@EA";
string s1 = Regex.Replace(s, @"@(.).", "$1");
string s2 = Regex.Replace(s, @"@.(.)", "$1");
Console.WriteLine(s1); // 输出:RYLQE
Console.WriteLine(s2); // 输出:AA22A
}
}
xzjxylophone 2009-11-28
  • 打赏
  • 举报
回复
up 学习了
jiangshun 2009-11-28
  • 打赏
  • 举报
回复
            string html = @"@RA@YA@L2@Q2@EA";
Regex reg = new Regex(@"(?<=\@(?<s1>\w{1}))\w{1}");
MatchCollection mc = reg.Matches(html);
Console.WriteLine("/*\n------输出结果------------");
string s1 = "";
string s2 = "";
foreach (Match m in mc)
{
s1 += m.Groups["s1"].ToString();
s2 += m.Groups[0].ToString();
}

Console.WriteLine(s1+"\n"+s2);


Console.WriteLine("*/");

/*
------输出结果------------
RYLQE
AA22A
*/
huming_h 2009-11-28
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string s="@RA@YA@L2@Q2@EA".Replace("@","");
string s1 = "",s2="";
for (int i = 0; i < s.Length; i++)
{
if (i % 2 == 0)
{
s1 += s[i];
}
else
{
s2 += s[i];
}
}
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.Read();
}
huming_h 2009-11-28
  • 打赏
  • 举报
回复
有个简单的办法,
首先置换掉所有@符号
然后根据string的index来取需要的字符,基数和偶数的组合

62,264

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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