当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Match one ant regex but not other

问题描述:

I want to allow the following syntax for my ant property

foo_abc:bar1,bar2,blah|foo_def:bar1,bar2|s64lk:kj,34kj

I wrote the below ant condition to validate and store in legalmyprop. First matches matches the full valid string. The following not matches explicitly disallow any other special characters that user may try. The not matches does not seem to be working

<condition property="legalmyprop" value="${myprop}">

<and>

<matches string="${myprop}" pattern="^[^:,|]+:[^,:|]+(,[^:,|]+)*(\|[^:,|]+:[^:,|]+(,[^:,|]+)*)*$" />

<not> <matches string="${myprop}" pattern="^.*[ =}{.;)(]+\*^\\'].*$" /> </not>

<not> <matches string="${myprop}" pattern='^.*["].*$' /> </not>

</and>

</condition>

foo:bar, Fails as expected

foo_abc:bar1,bar2,blah|foo_def:bar1,bar2|s64lk:kj,34kj Passes as

expected

foo::bar| Fails as expected

foo:bar.abc Passes as unexpected

foo:bar=abc Passes as unexpected

Any ideas how to fix the regex?

网友答案:

Let's take a closer look at your current not match condition:

^.*[ =}{.;)(]+\*^\\'].*$
   ^        ^
   |        |
 Start     End of the character class

The current char class is [ =}{.;)(] and you wanted it to be:

[ =}{.;)(\]+\*^\\']
         ^

To work as expected

分享给朋友:
您可能感兴趣的文章:
随机阅读: