时间:2017年06月07日 | 作者 : aaronyang | 分类 : AY梦 | 浏览: 6784次 | 评论 1 人
AY表达式: AY定义的字符串语法,跟正则,数据库模糊查询的那种思路差不多,但是AY表达式可以处理数据并返回
下面AY来介绍该成员:
所属命名空间:Ay.Framework.WPF.AYUIExpression
开发周期:30天
版本:3.1.0.0
成员:
AyExpression类 Ay表达式处理核心类
AyExpressionConverter类 给WPF界面提供转换器,方便TextBox的Text属性经过 AyExpression处理,参数为 ay表达式字符串
AyExpressionExtendMethod 类 给用户提供快捷的操作方法
历史版本:
1.0 基础 占位符,基础*替换功能
1.1 拓展* 号功能,增加 @#&支持@(number) #(number) &(number)
1.2 移除&,增加%替换
1.3 替换支持 \) 了,规则判断,在一个*(替换表达式) 最多查找30次 \),超过30次,提示错误。 修复部分bug
2.0 增加前置表达式和后置表达式概念,增加大型语法。
前置/后置 替换规则 *(处理的字符数M 处理方向 [+N,表示处理N个符合要求的字符串,不含有+号,表示处理第N个字符串]^被替换的字符串 -> 被替换后的字符串 )
如果N等于 ? 表示 所有匹配到的字符串都要处理.
如果M等于? 表示剩余的所有字符
当下: Ay表达式处于测试截断,是融入AYUI框架的,对外不单独分离,作为AYUI用户的 特别部分,也算是AYUI的特色
当然,这种表达式的目的,你一看就清楚了,你也可以自己去实现,不要问我代码是否开源。AYUI源码需要用钱购买。
相关文章:length用法 : 查看
2016-10-9 01:13:39
修复num规则下:
新增 num(?,4)[@ge 0] 问号代表整数位随便,后面数字,这里是4,代表最多4位小数。整数位置,不验证,但是至少1位的。修复部分提示的问题。
2016-8-23 11:49:58
修复末尾 *(?) 当字符数量不满足时候,显示了?号,同理修复#(数字) @(数字) %(数字) *(数字)
2016-8-6 21:17:35
AY表达式:num用法
支持整数,小数的位数控制,是否没有小数,还有值范围
控制台测试:
private static void AyValidate(string text4, string express4) { AyFormResult result4 = text4.ToAyExpressionFormResult(express4); Console.WriteLine("原字符串:" + text4); Console.WriteLine("AY表单表达式:" + express4); if (result4.Result) Console.WriteLine("验证结果:通过"); else Console.WriteLine("验证结果:失败! " + result4.Error); Console.WriteLine(); }
DEMO :
string text1 = "+3770.77"; string express1 = "required;num"; AyValidate(text1, express1); string text20 = "-770.0"; string express20 = "required;num(3)"; AyValidate(text20, express20); string text2 = "-370.7"; string express2 = "required;num(3,2)"; AyValidate(text2, express2); string text3 = "580"; string express3 = "required;num(3)[@ge 0,@le 200]"; AyValidate(text3, express3); string text4 = "3132"; string express4 = "required;num[@g 0,@le 1000]"; AyValidate(text4, express4);
纯num写法 代表数字验证
num(指定数字) 代表几位整数
num(整数位数,小数位数) 控制位数
num(3)[@ge 0,@le 200] 配合 [] 控制数字行为,注意后面的条件是每一个都要满足,不然返回false
num[@g 0,@le 1000] 也可以不指定位数,直接限制数字行为
关于数字行为控制:有6个运算符,
ay也提供了几个便捷的数字使用规则,比如
integer是大于0的正整数, integerZero是大于等于0的正整数
age是0-300的之间的数字,这里除了required和length和num比较特殊,也比较大型。当然用户可以快捷的拓展表达式
方便在AyForm表单控件快速使用
下面是3个示例,第一个简单,直接regexexpression指定正则,第二个指定自己逻辑,第三个是可以截取你自己额表达式,自己定义自己的规则,解析
public class AyFormNormalValidator : AyFormValidator { public AyFormNormalValidator() { Example = "测试验证"; ErrorInfo = "测试用的,你输入错误了"; RegexExpression = @"^[0-9]$"; } } public class AyFormNormal2Validator : AyFormValidator { public AyFormNormal2Validator() { Example = "测试验证2"; ErrorInfo = "测试用的,你输入错误了2"; CustomFunc = (text) => { int dd = text.Length; if (dd < 3) { return true; } else { ErrorInfo = "输入的文本不能超过3个字符"; return false; } }; } } public class AyFormDiff3Validator : AyFormValidator { public AyFormDiff3Validator() { Example = "完全自定义自己的规则"; ErrorInfo = "这规则太难"; } public override bool Validate(string text, string expression = "") { //expression 这里可以获取你定义的表达式,自己在这里可以额外处理 int dd = text.Length; if (dd < 3) { return true; } else { ErrorInfo = "输入的文本不能超过3个字符"; return false; } } }
使用方法
AyExpression.CustomValidators.Add("nor", new AyFormNormalValidator()); string text10 = "13"; string expres10 = "nor"; AyValidate(text10, expres10); AyExpression.CustomValidators.Add("nor2", new AyFormNormal2Validator()); string text11 = "1"; string expres11 = "nor2"; AyValidate(text11, expres11); AyExpression.CustomValidators.Add("nor3", new AyFormDiff3Validator()); string text12 = "1"; string expres12 = "nor3"; AyValidate(text12, expres12);
效果图:
一旦在AYUI项目定义好了,直接在AyFormInput可以快速指定:
验证效果:
你完全不用后台定义啥规则,验证了。非常的方便。
测试其他的AY表达式
2016-7-29 20:34:37
新增 前置和后置表达式。详细理解看代码注释
效果图:
代码DEMO20 :
/// 前置/后置 替换规则 *(处理的字符数 处理方向 [+N,表示处理N个符合要求的字符串,不含有+号,表示处理第N个字符串]^被替换的字符串 -> 被替换后的字符串 ) /// 如果N等于 ? 表示 所有匹配到的字符串都要处理 string demo20 = "测试人员、开发人员、管理人员、15,17,19,20,100"; string ayexpression1 = "*(? ^< 1^, -> 和)"; string ayexpression2 = "*(? ^< 2^, -> 和)"; string ayexpression3 = "*(? ^< 1^, -> and )"; string ayexpression4 = "*(? ^> 1^, -> 和)"; string ayexpression5 = "*(? ^> 2^, -> 和)"; string ayexpression6 = "*(? ^> 1^, -> and )"; Console.WriteLine("原字符串:" + demo20); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression1); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression1)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression2); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression2)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression3); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression3)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression4); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression4)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression5); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression5)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression6); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression6)); //处理接下来所有的字符串,从左往右处理 处理前2个","字符串,找到后替换成"和" 返回处理后的字符串 string ayexpression7 = "*(? ^> +2^, -> 和)"; //处理接下来所有的字符串,从右往左处理 处理前2个","字符串,找到后替换成"和" 返回处理后的字符串 string ayexpression8 = "*(? ^< +2^, -> 和)"; //处理接下来所有的字符串,从右往左处理 处理所有","字符串,找到后替换成" and " 返回处理后的字符串 string ayexpression9 = "*(? ^< ?^, -> and )"; Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression7); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression7)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression8); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression8)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression9); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression9)); //处理接下来7个字符长度的字符串,截取7个字符串后,从左往右处理 处理前2个","字符串,找到后替换成" and " 返回处理后的字符串 string ayexpression10 = "*(7 ^> +2^, -> 和)"; //处理接下来8个字符长度的字符串,截取8个字符串后,从右往左处理 处理前2个","字符串,找到后替换成" and " 返回处理后的字符串 string ayexpression11 = "*(8 ^< +2^, -> 和)"; //处理接下来9个字符长度的字符串,截取9个字符串后,从右往左处理 第2个","字符串,找到后替换成" and " 返回处理后的字符串 string ayexpression12 = "*(9 ^< 2^, -> and )"; //处理接下来10个字符长度的字符串,截取10个字符串后,从左往右处理 第2个","字符串,找到后替换成" and " 返回处理后的字符串 string ayexpression13 = "*(10 ^> 2^, -> and )"; //处理接下来40个字符长度的字符串,截取40个字符串后,从左往右处理 第2个","字符串,找到后替换成" and " 返回处理后的字符串 string ayexpression14 = "*(40 ^> 2^, -> and )"; Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression10); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression10)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression11); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression11)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression12); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression12)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression13); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression13)); Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression14); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression14)); //尝试2 20的长度 “测试人员、开发人员、管理人员、15,17” 第21位是, 按照规则,从左往右 第1个替换 //就是17后面的 逗号被替换 string ayexpression15 = "*(20)*(? ^> 1^, -> ☆开心AY☆)"; Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression15); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression15)); string ayexpression16 = "*(? ^> ?^人员 -> 工程师)"; Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression16); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression16)); string ayexpression17 = @"*(? ^> ?^人员 -> \(职位\))"; Console.WriteLine(); Console.WriteLine("AY表达式:" + ayexpression17); Console.WriteLine(demo20.ToAyExpressionValue(ayexpression17));
修复了很多bug
2016-7-29 15:04:22
替换支持 \) 了,规则判断,在一个*(替换表达式) 最多查找30次 \),超过30次,提示错误
2016-7-29 09:20:59
由于&符号在wpf中是特殊符号,我就换成%替换它了。
2016-7-27 16:56:46
新增 @ # & 增加重复表达式,支持 @(number) #(number) &(number)
Demo 11 使用#(number)
string viewMask11 = "#(4) #(4) #(4) #(4) #(4)"; string userText11 = "6228480402564890018"; var d11 = AyExpression.GetMaskedValue(viewMask11, userText11); Console.WriteLine(d11.ToString());
效果图:
DEMO12 使用@(number)
string viewMask12 = @"AY-@(2) @(3) *(?)"; string userText12 = "AB22016CDEFG"; var d12 = AyExpression.GetMaskedValue(viewMask12, userText12); Console.WriteLine(d12.ToString());
DEMO13 使用 &(number)
DEMO14 使用 拓展方法,多次使用链式处理
先去中杠,然后取GUID中的数字
string viewMask14 = @"&(32)"; string userText14 = "88679C65-ADB1-42E2-9D3B-10589CACD276"; var d14 = userText14.ToAyExpressionValue(viewMask14).ToAyExpressionValue("#(1000)"); Console.WriteLine(d14.ToString());
DEMO15 电话号码格式化
DEMO16 百分比
=================================================
为了方便测试,AY单独分离,测试使用
这里我新建控制台的。
new AyExpression().Helper();
DEMO1:
使用#号,处理银行卡,遇到非#的规则,直到找到为止
代码1: 使用#号代表数字
string viewMask = "#### #### #### #### ####"; string userText = "6228480402564890018"; var d = AyExpression.GetMaskedValue(viewMask, userText); Console.WriteLine(d.ToString());
代码2: 使用\U 转换大写字母 使用\L 转换小写 ,教你使用 ToAyExpressionValue拓展方法
string viewMask1 = @"\U\U\L## ## ## ##\U"; string userText1 = "dzA87555600a03"; var d1 = userText1.ToAyExpressionValue(viewMask1); Console.WriteLine(d1.ToString());
代码3: 内容追加,包括前后 ,使用*号, *(number) 重复指定number个*号,生成表达式,比如 *(3) 等同于 ***
string viewMask2 = @"AY-\U\U\L*(8)@AYUI**\(来自www.ayjs.net\)"; string userText2 = "dzA87555600a03"; var d2 = AyExpression.GetMaskedValue(viewMask2, userText2); Console.WriteLine(d2.ToString());
代码4: 使用 *(13 -> \*) 替换规则 表示接下来的 13个字符都替换成*号 后面的替换的符号随变写的,只要不是括号,括号有点问题,可以多个字符。
string viewMask3 = @"##*(4 -> \*)###"; string userText3 = "875556003"; var d3 = AyExpression.GetMaskedValue(viewMask3, userText3); Console.WriteLine(d3.ToString());
代码5:使用*(?) 表示剩余未知数量的字符,也就是剩余所有,用于ay表达式的末尾
string viewMask4 = @"###*(4 -> \*)*(?)"; string userText4 = "15255112050"; var d4 = AyExpression.GetMaskedValue(viewMask4, userText4); Console.WriteLine(d4.ToString()); Console.WriteLine(d4.Length);
代码6:综合一下,AY表达式处理 身份证的 DEMO
string viewMask5 = @"##########*(4 -> \*)*(?)"; string userText5 = "650101198604183434"; var d5 = AyExpression.GetMaskedValue(viewMask5, userText5); Console.WriteLine(d5.ToString());
代码7:使用*(? -> \*) 表示剩余所有字符 都替换成 * 号 这里的*是特殊的符号,所以需要\* 转义
string viewMask6 = @"**(? -> \*)"; string userText6 = "张杨洋"; var d6 = AyExpression.GetMaskedValue(viewMask6, userText6); Console.WriteLine(d6.ToString()); string userText7 = "杨洋"; var d7 = AyExpression.GetMaskedValue(viewMask6, userText7); Console.WriteLine(d7.ToString());
代码8: 时间处理DEMO
string viewMask8 = @"####年##月##日 ##:##:##"; string userText8 = "20160812235759"; var d8 = AyExpression.GetMaskedValue(viewMask8, userText8); Console.WriteLine(d8.ToString());
代码9:&符号使用,表示 字母或者数字,处理逻辑跟#号和@符号是一样的。
string viewMask9 = @"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"; string userText9 = "88679C65-ADB1-42E2-9D3B-10589CACD276"; var d9 = AyExpression.GetMaskedValue(viewMask9, userText9); Console.WriteLine(d9.ToString()); Console.WriteLine(d9.Length);
代码10: 使用$ 符号提前终止 匹配处理
string viewMask10 = @"###*(4 -> \*)*(2)$"; string userText10 = "15255112050"; var d10 = AyExpression.GetMaskedValue(viewMask10, userText10); Console.WriteLine(d10.ToString()); Console.WriteLine(d10.Length);
这个DEMO,取了 前4个数字,然后从第5个开始替换4个字符为*号,然后保留后面的2个字符,使用$终止了后面的处理,$跟*(?) 一样 用于ay表达式的末尾
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
以上 为AY表达式 1.1
AY表达式由 3部分组成
以上是
第一部分的开发: 用于常见的掩码处理
第二部分,常见的字符处理(开发中)
第三部分,AY表单表达式,用于验证表单,这一块,AY还在开发,期待AyForm和AyInput控件吗,让你的表单方面的需求的 开发前所未有的快。
AYUI4.2:特征: 动画系统,表单系统,AY表达式,兼容Winform控件,开发更简洁,面向开发者
AYUI4.1:特征: 全新重构3.X的项目,去static化,用了享元模式重构很多代码,AYUI新渐变色,配置自由化, 开发 使用vs扩展插件,开发,更快速简单。
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
抖音:wpfui 工作wpf,目前主maui
招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰
目前在合肥市企迈科技就职
AYUI8全源码 Github地址:前往获取
杨洋(AaronYang简称AY,安徽六安人)和AY交流
高中学历,2010年开始web开发,2015年1月17日开始学习WPF
声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费
不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo
查看捐赠AYUI7.X MVC教程 更新如下:
第一课 第二课 程序加密教程
已有1位网友发表了看法:
还是很复杂的嘛?
发表评论