一个正则表达式的测试工具

用Java写的一个正则表达式测试工具,对下载的一个工具做了改进。

 

写这个的初衷是因为项目中要为struts的html标签加上styleId属性,其属性值一般与property属性值相同。由于页面很多手工修改很可能会出差错工作量也很大,虽然这项工作不是由我来做,但是我建议可以用正则表达式替换来做,可以简单很多。写正则表达式的任何就落在我的头上了,这个正则表达式不会很简单,我需要能够快速的测试正则表达式。而且eclipse或ultraedit等工具一般只接受单行正则表达式,不接受带注释的正则表达式,但是在开发过程中使用带注释的正则表达式是很重要的,将所有的正则表达式写在一行,一旦正则表达式太长很容易把自己搞糊涂,因此我需要将带注释的正则表达式转化成单行的不带注释的正则表达式。

 

 

要匹配一个简单的属性,属性包括属性名或属性值,属性值可以包含在单引号或双引号中:

\w+\s*=\s*"[^"]*"|'[^']*'

 不幸的是它不能正确匹配:property="<%="prop"+i%>",因此要匹配一个属性:

\w+\s*=\s*(?:"<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')

 (?:)表示不捕获该组。

 

匹配标签的开始部分,(?!\s+styleId),向前负匹配,表示如果已经存在styleId属性就不用替换了

<html:(text|select|hidden)(?!\s+styleId)

匹配property属性之前或之后的部分:

(?:\s+\w+\s*=\s*(?:"<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId))*

匹配property属性:

property\s*=\s*("<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId)

 

最终结果:

( # 捕捉property属性之前的所有部分,用于替换
  <html:(text|select|hidden)(?!\s+styleId)           #起始标签,不包含styleId
  (?:\s+\w+\s*=\s*(?:"<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId))*    #property属性之前的属性
  \s+ #属性之间的空格
  property\s*=\s*("<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId) #property属性
)
( # 捕捉property属性之后的所有部分,用于替换
  (?:\s+\w+\s*=\s*(?:"<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId))*    #property属性之后的属性
  \s* #开始标签结尾部分可选的空格
  (?:/>|>)
)

将它转换成不带注释的正则表达式:

*"|'[^']*')(?!\s+styleId))*\s+property\s*=\s*("<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId))((?:\s+\w+\s*=\s*(?:"<%=(?:[^%]*|[^>]*)%>"|"[^"]*"|'[^']*')(?!\s+styleId))*\s*(?:/>|>))

替换表达式为: $1 styleId=$3$4

 

 

测试输入:

 
<html:text property="property">text</html:text>
<html:text name="something" property="property" other=""/>

<html:hidden name="something"   property ="<%="citation[" + index +"].displayFlag"%>"  other=""/>
<html:hidden  property ="<%="citation[" + index +"].displayFlag"%>"/>

 使用编辑器的正则表达式替换功能(使用ultraedit需将正则表达式的引擎设置为perl),替换结果将为:

<html:text property="property" styleId="property">text</html:text>
<html:text name="something" property="property" styleId="property" other=""/>

<html:hidden name="something"   property ="<%="citation[" + index +"].displayFlag"%>" styleId="<%="citation[" + index +"].displayFlag"%>"  other=""/>
<html:hidden  property ="<%="citation[" + index +"].displayFlag"%>" styleId="<%="citation[" + index +"].displayFlag"%>"/>
 
Regex Testor Version 1.02 Copyright (c) 2013 Fiery Red - flameleo 我们在使用正则表达式( regex: regular expression )的过程中,经常发现正则表达式的语法很令人头疼,即使对经常使用它的人来说也是如此。对于刚接触正则表达式的人来说多练习,多使用,才能熟练掌握正则表达式。 由于难于读写,容易出错,且需要反复练习。所以找一种工具对正则表达式对我们脑中构思的regex进行测试是很有必要的。 1.特点 a.适合初学者,在不断测试用学习如何使用正则表达式。 b.可以分组保存,测试中用到的正则表达式。 c.本工具使用最常见的regex。以下是简单示例: a|b Matches a or b gr(a|e)y Matches a or e . Matches any single character [abc] Matches a single character a, b or c [^abc] Matches any single character except a, b or c [a-z] Matches a single charactor in the range a to z [a-zA-Z] Matches a single charactor in the range a to z or A to Z ^ Matches the start of the filename $ Matches the end of the filename * Matches the preceding element zero or more times ? Matches the preceding element zero or one times + Matches the preceding element one or more times {x} Matches the preceding element x times {x,} Matches the preceding element x or more times {x,y} Matches the preceding element between x and y times 值得一提的是()代表子匹配,有些环境中gr(a|e)y Matches gray or grey 还支持许多常见的转义字符 \b,\B,\c,\d,\D,\f,\n,\r,\s,\S,\t,\v,\w,\W,\x,\u 具体详见附件 Regular Expression Syntax1.html 2.功能介绍 a.界面上显示提供regex输入框和原文本框,点击[模式匹配]按钮后,会在右侧输出结果,包括匹配字符串列表和文本。 b.对于测试中一些有用的regex,点击[insert]按钮添加到模式列表,以备日后使用。你可以位该regex添加描述分组,该信息会在程序结束后保存在CustomPatternInfo.ini文件中。 c.可以参考Readme_1.jpq和Readme_2.jpq图片介绍。 3.有待改进 a.界面布局和控件友好型和交互性。 b.界面功能提供regex语法支持。(暂时可以通过导入附件RegexSystax.ini到CustomPatternInfo.ini中) 4.意见反馈 a.请将您的宝贵意见反馈到 FieryRed_2012@163.com 附件: Readme.txt Readme_1.jpg Readme_2.jpg Regular Expression Syntax.html RegexSystax.ini
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值