开发者

Regular expression for groovy domain class property

开发者 https://www.devze.com 2023-03-26 08:48 出处:网络
I have a property of type string.It may accept all numeric and alphanumeric values. And i need an expr开发者_StackOverflow中文版ession which must contains \"-[0-9]\" at end of the string

I have a property of type string.It may accept all numeric and alphanumeric values. And i need an expr开发者_StackOverflow中文版ession which must contains "-[0-9]" at end of the string Like A-a-A2-AXY-1 and the following format

XX-XXX-X**-11** any string and at last **-number**


If I understand you right, the pattern you require is something like:

/[0-9A-Za-z]+(-[0-9A-Za-z]+)*-[0-9]/

I wrote a quick test:

valid = [ 'A-a-A2-AXY-1', 'A-a-A2-A2Y-11', '14-a-A2-A2Y-11' ]
invalid = [ '-a-A2-AXY-A', 'A-a-A2-AXY-', 'A-a-A2-AXY-B' ]

pattern = /[0-9A-Za-z]+(-[0-9A-Za-z]+)*-[0-9]+/

valid.each { println "$it ${it ==~ pattern}" }
invalid.each { println "$it ${it ==~ pattern}" }

which outputs:

A-a-A2-AXY-1 true
A-a-A2-A2Y-11 true
14-a-A2-A2Y-11 true
-a-A2-AXY-A false
A-a-A2-AXY- false
A-a-A2-AXY-B false

So seems to work

The pattern can probably be shortened by use of wildcards

0

精彩评论

暂无评论...
验证码 换一张
取 消