groovy - Groovy 正则表达式解析值
问题描述
我正在尝试使用正则表达式解析以下项目
输入字段是
def details = "jurisdictionOfIncorporationCountryName=GB, l=Cheshunt, st=Herts, c=GB"
需要的输出是
- 位置 = Chesthunt
- 状态 = 赫特
- 国家 = 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] }
这~/l=([^\s,]+)/
会查找以 开头的l=
内容,然后一直捕获到空格或逗号
推荐阅读
- r - 在 R 中一起使用 cast 和 split 字符串函数
- mysql - MySQL SQL 创建具有列和行总计的临时表
- python - 在 pyspark 中聚合和连接两个字符串时出现问题
- core-data - 抽象实体有什么特别之处?
- angular - 我们如何在 Angular 7 中的 URL 中编码 # 并发送到后端服务器,因为后端不会收到 URL 中 # 之后的任何内容
- python - 如何拆分堆叠特征的训练测试数据?
- kubernetes - Kubernetes:每个集群或每个 statefulset 或 replicaset 需要完成一次的任务
- php - 输入文本框时不显示自动完成,但控制台中显示值
- c++ - 如何对继承的文件使用前向声明
- amazon-s3 - logstash 输出 s3 文件名