明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2694|回复: 8

如何用正则表达式判断角度字符的正确

[复制链接]
发表于 2012-7-20 17:05 | 显示全部楼层 |阅读模式
1明经币
如有格式化的角度字符“145-56-25”,如何用正则表达式判断角度字符的正确。
要求度大于等于0,小于360;分秒小于60即可。如果正确返回TRUE,否则返回FALSE。

最佳答案

查看完整内容

直接正则有点复杂。。。好久没用快忘了
发表于 2012-7-20 17:05 | 显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2012-7-20 19:02 编辑

直接正则有点复杂。。。好久没用快忘了
  1.            var s = "135-15-15";
  2.             var pat = "^(\\d{1,2}|[1,2]\\d{1,2}|3[0-5]\\d)-(\\d{1}|[1-5]\\d)-(\\d{1}|[1-5]\\d)$";

  3.             var m = Regex.Match(s, pat);
  4.             if(m.Success)
  5.             {
  6.                 int d = int.Parse(m.Groups[1].Value);
  7.                 int f = int.Parse(m.Groups[2].Value);
  8.                 int ss = int.Parse(m.Groups[3].Value);
  9.             }
复制代码

回复

使用道具 举报

 楼主| 发表于 2012-7-20 18:38 | 显示全部楼层
出错啦,不知是什么原因,正则表达式一点也不懂!

************** 异常文本 **************
System.ArgumentException: 正在分析“^([url=file://d%7b1,2%7d%7c[1,2]//d%7B1,2%7D%7C3[1-5]//d)-(//d%7B1%7D%7C[1-5]//d)-(//d%7B1%7D%7C[1-5]//d)$]\d{1,2}|[1,2]\d{1,2}|3[1-5]\d)-(\d{1}|[1-5]\d)-(\d{1}|[1-5]\d)$[/url]”- ) 过多。
   在 System.Text.RegularExpressions.RegexParser.ScanRegex()
   在 System.Text.RegularExpressions.RegexParser.Parse(String re, RegexOptions op)
   在 System.Text.RegularExpressions.Regex..ctor(String pattern, RegexOptions options, Boolean useCache)
   在 System.Text.RegularExpressions.Regex.Match(String input, String pattern)
   在 Measure_computation.Form1.DigitalFormatRight(String strNumber) 位置 d:\my documents\visual studio 2010\Projects\Measure computation\Measure computation\Form1.cs:行号 107

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

发表于 2012-7-20 19:05 | 显示全部楼层
"^(\\d{1,2}|[1,2]\\d{1,2}|3[0-5]\\d)-(\\d{1}|[1-5]\\d)-(\\d{1}|[1-5]\\d)$";
论坛的解析有点问题。。。
回复

使用道具 举报

发表于 2012-7-20 19:08 | 显示全部楼层
如果解析这样的格式"012-01-01"用
"^([0-2]\\d{1,2}|3[0-5]\\d)-([0-5]\\d)-([0-5]\\d)$";
回复

使用道具 举报

 楼主| 发表于 2012-7-20 19:09 | 显示全部楼层
就是数据超出限制时还是报错,如输入369-25-55时

************** 异常文本 **************
System.FormatException: 输入字符串的格式不正确。
   在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   在 System.Int32.Parse(String s)
   在 Measure_computation.Form1.DigitalFormatRight(String strNumber) 位置 d:\my documents\visual studio 2010\Projects\Measure computation\Measure computation\Form1.cs:行号 108
   在 Measure_computation.Form1.maskedTextBox1_KeyUp(Object sender, KeyEventArgs e) 位置 d:\my documents\visual studio 2010\Projects\Measure computation\Measure computation\Form1.cs:行号 30
   在 System.Windows.Forms.Control.OnKeyUp(KeyEventArgs e)
   在 System.Windows.Forms.MaskedTextBox.OnKeyUp(KeyEventArgs e)
   在 System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   在 System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
   在 System.Windows.Forms.MaskedTextBox.ProcessKeyMessage(Message& m)
   在 System.Windows.Forms.Control.WmKeyChar(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.TextBoxBase.WndProc(Message& m)
   在 System.Windows.Forms.MaskedTextBox.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
回复

使用道具 举报

 楼主| 发表于 2012-7-20 19:11 | 显示全部楼层
我的格式统一用000-00-00
回复

使用道具 举报

发表于 2012-7-20 19:12 | 显示全部楼层
本帖最后由 雪山飞狐_lzh 于 2012-7-20 19:13 编辑

数据超出限制时m.Success返回false 这个是你要求的额:

要求度大于等于0,小于360;分秒小于60即可。如果正确返回TRUE,否则返回FALSE

还有。。。上面的pat有小改动
回复

使用道具 举报

 楼主| 发表于 2012-7-20 19:22 | 显示全部楼层
非常感谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-4 13:10 , Processed in 0.291713 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表