Groovy 正则表达式解析值
Groovy regex parse value
我正在尝试使用正则表达式解析以下项目
输入字段是
def details = "jurisdictionOfIncorporationCountryName=GB, l=Cheshunt, st=Herts, c=GB"
需要的输出是
- 位置 = 猎豹
- 州=赫茨
- 国家/地区 = GB
我试图检索状态的代码是
def location= subject =~/([l][=])\w+/
当我打印位置值时,我得到这个作为输出
r[pattern=([l][=])\w+ region=0,192 lastmatch=l=Cheshunt]
我尝试使用解析方法
if (location) {
location = location[0][0]
}
我得到的输出是 l=chesthunt,我只需要获取 chesthunt。你能帮忙吗,因为我是 groovy/regex
的新手鉴于:
def details = "jurisdictionOfIncorporationCountryName=GB, l=Cheshunt, st=Herts, c=GB"
你可以这样做:
def location = details.find(~/l=([^\s,]+)/) { it[1] }
this ~/l=([^\s,]+)/
查找以 l=
开头的内容,然后捕获直到空格或逗号