Groovy regex/pattern matching

感情迁移 提交于 2021-01-05 09:24:05

问题


How do we do regex matching in groovy, what will be the regex in groovy for below example?

Example : f2376 Regex: (anyLetter)(followed by 4 digits)

回答1:


Pretty simple with groovy

"f1234" ==~ /[a-z]\d{4}/

Note that the regex [a-z]\d{4} means any of the characters a-z once, followed by exactly 4 digits, and can be probably be used with any language that handles regex, not just groovy.

In my console I tested for just lower case letters, but to handle upper case too just do

"f1234" ==~ /[a-zA-Z]\d{4}/

enter image description here



来源:https://stackoverflow.com/questions/7772497/groovy-regex-pattern-matching

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!