gpt4 book ai didi

java - 获取某个正则表达式中字符串的值

转载 作者:行者123 更新时间:2023-12-01 12:07:15 26 4
gpt4 key购买 nike

我有这样的代码:

String s1 = "((${adapterKind=VMWARE, resourceKind=VirtualMachine, metric=disk|usage_average}*${this, metric=summary|oversizedVMCount})+${this, metric=summary|poweredOffVMCount})";
//Some code

I want output inside

String[] s2 = {${adapterKind=VMWARE, resourceKind=VirtualMachine, metric=disk|usage_average}, ${this, metric=summary|oversizedVMCount}, ${this, metric=summary|poweredOffVMCount}};

基本上我想获取所有带有“${...}”的值。我怎样才能实现这个目标?

最佳答案

String s1 = "((${adapterKind=VMWARE, resourceKind=VirtualMachine, metric=disk|usage_average}*${this, metric=summary|oversizedVMCount})+${this, metric=summary|poweredOffVMCount})";
Pattern p = Pattern.compile("\\$\\{[^}]*\\}");
Matcher m = p.matcher(s1);
while(m.find())
//here you have the captured texts,
//you can put into array/collection as you like
System.out.println(m.group());

此输出:

${adapterKind=VMWARE, resourceKind=VirtualMachine, metric=disk|usage_average}
${this, metric=summary|oversizedVMCount}
${this, metric=summary|poweredOffVMCount}

关于java - 获取某个正则表达式中字符串的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com