正则表达式高手请进

Zanvimocvy 2013-10-23 03:14:19
求一个正则表达式,能验证十六进制数,并自动纠正错误(不是屏蔽错误然后返回的那种),
比如我发送一个十六进制数:adt 12 ce 63 02251 2634
然后经过正则表达式纠正后 :AD 12 CE 63 02 25 01 26 34
请教高手,正则表达式怎么表达
...全文
153 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Zanvimocvy 2013-10-23
  • 打赏
  • 举报
回复
引用 10 楼 q107770540 的回复:

//.NET FREAMEWORK3.5+版本 编译通过
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "adt 12 ce 63 02251 2634";
            str = Regex.Replace(Regex.Replace(str, @"(?i)[^a-f\d\s]+", ""), "\\w{3,}",
                                    m=> string.Join(" ", Regex.Split(m.Value, @"(?<=\G\w{2})(?!$)").Select(x => x.PadLeft(2, '0')).ToArray())).ToUpper();
            Console.WriteLine(str); //AD 12 CE 63 02 25 01 26 34
            Console.ReadKey();
        }
    }
}
谢谢你,刚刚又仔细看了下,找到问题了。正准备结贴的,版主又来帮忙了,真心谢谢你。
q107770540 2013-10-23
  • 打赏
  • 举报
回复

//.NET FREAMEWORK3.5+版本 编译通过
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "adt 12 ce 63 02251 2634";
            str = Regex.Replace(Regex.Replace(str, @"(?i)[^a-f\d\s]+", ""), "\\w{3,}",
                                    m=> string.Join(" ", Regex.Split(m.Value, @"(?<=\G\w{2})(?!$)").Select(x => x.PadLeft(2, '0')).ToArray())).ToUpper();
            Console.WriteLine(str); //AD 12 CE 63 02 25 01 26 34
            Console.ReadKey();
        }
    }
}
Zanvimocvy 2013-10-23
  • 打赏
  • 举报
回复
这种错误,不可能是版本问题吧
Zanvimocvy 2013-10-23
  • 打赏
  • 举报
回复
错误 1 无法将 lambda 表达式 转换为类型“string”,因为它不是委托类型 C:\Documents and Settings\Administrator\桌面\测试\SerialportSample(1)+2008\SerialportSample\SerialportSample\Form1.cs 161 37 SerialportSample 错误 2 “string[]”不包含“Select”的定义,并且找不到可接受类型为“string[]”的第一个参数的扩展方法“Select”(是否缺少 using 指令或程序集引用?) C:\Documents and Settings\Administrator\桌面\测试\SerialportSample(1)+2008\SerialportSample\SerialportSample\Form1.cs 161 59 SerialportSample
Zanvimocvy 2013-10-23
  • 打赏
  • 举报
回复
引用 4 楼 UR_Not_Alone 的回复:
什么vs版本?
vs2010
q107770540 2013-10-23
  • 打赏
  • 举报
回复
.NET版本问题
Zanvimocvy 2013-10-23
  • 打赏
  • 举报
回复
引用 3 楼 q107770540 的回复:
我的代码是没有问题的,不论你相信不相信,反正我是信了
截图就很清楚了。
夜轻风 2013-10-23
  • 打赏
  • 举报
回复
什么vs版本?
q107770540 2013-10-23
  • 打赏
  • 举报
回复
我的代码是没有问题的,不论你相信不相信,反正我是信了
Zanvimocvy 2013-10-23
  • 打赏
  • 举报
回复
引用 1 楼 q107770540 的回复:
void Main()
{
	string str="adt 12 ce 63 02251 2634";
	str=Regex.Replace(Regex.Replace(str,@"(?i)[^a-f\d\s]+",""),"\\w{3,}",
							m=>string.Join(" ",Regex.Split(m.Value,@"(?<=\G\w{2})(?!$)").Select(x=>x.PadLeft(2,'0')).ToArray())).ToUpper();
	Console.WriteLine(str); //AD 12 CE 63 02 25 01 26 34

}
版主,我用你的表达式怎么报错了。你再看下代码,是不是什么地方漏掉了。
q107770540 2013-10-23
  • 打赏
  • 举报
回复
void Main()
{
	string str="adt 12 ce 63 02251 2634";
	str=Regex.Replace(Regex.Replace(str,@"(?i)[^a-f\d\s]+",""),"\\w{3,}",
							m=>string.Join(" ",Regex.Split(m.Value,@"(?<=\G\w{2})(?!$)").Select(x=>x.PadLeft(2,'0')).ToArray())).ToUpper();
	Console.WriteLine(str); //AD 12 CE 63 02 25 01 26 34

}

110,539

社区成员

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

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

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