Java正则表达式与花括号

huangapple go评论81阅读模式
英文:

java regex with curly braces

问题

我在解决这个特定问题时遇到了困难

输入: [{"acks":"6"},{"acks":"7"},{"acks":"8"}]
正则表达式: [{]*["acks:"]
当前输出: [6},7},8}]
期望输出: [6,7,8]

我遇到了结束花括号的问题。

提前感谢您的帮助!

英文:

I am having hard time solving this particular problem

input: [{"acks":"6"},{"acks":"7"},{"acks":"8"}]
regex: [\{]*["acks:"]
current output: [6},7},8}]
desired output: [6,7,8]

I am facing problem with the ending curly braces.

Thank you for helping me in advance!!

答案1

得分: 1

你可以尝试这个正则表达式 (?:\"[\\w]+\":\")(\\d+)

import java.util.regex.*;
import java.util.*;
import java.util.function.Supplier;

class HelloWorld {
    public static void main(String[] args) {

    Pattern p = Pattern.compile("(?:\"[\\w]+\":\")(\\d+)");
    Matcher m = p.matcher("[{\"acks\":\"6\"},{\"acks\":\"7\"},{\"acks\":\"8\"}]\t");  

    List<String> llist = new LinkedList<String>();
    while (m.find()) {    
        llist.add(m.group(1));
    }    
    if(llist.size() == 0) {    
        System.out.println("No match found.");    
    } else {
        System.out.println("Output : " + llist.toString());
    }
   }
}
英文:

You can try this regexp (?:\"[\\w]+\":\")(\\d+)

import java.util.regex.*;
import java.util.*;
import java.util.function.Supplier;

class HelloWorld {
    public static void main(String[] args) {

    Pattern p = Pattern.compile("(?:\"[\\w]+\":\")(\\d+)");
    Matcher m = p.matcher("[{\"acks\":\"6\"},{\"acks\":\"7\"},{\"acks\":\"8\"}]\t");  

    List<String> llist = new LinkedList<String>();
    while (m.find()) {    
        llist.add(m.group(1));
    }    
    if(llist.size() == 0) {    
        System.out.println("No match found.");    
    } else {
        System.out.println("Output : " + llist.toString());
    }
   }
}

答案2

得分: 0

以下是翻译好的部分:

如果你只想要去掉 [,],, 和数字,你可以使用以下正则表达式将其他所有内容替换为空字符串:[^\d,[\]]+

如果你只需要从具有单一字段 "acks" 的对象中提取数字,你可以使用 $1 替换\{"acks":"(\d+)"\}
演示在这里

英文:

It is unclear precisely what you are trying to do.

If all you want is to leave out [,],, and digits, you can replace everything else with empty string using the following regex: [^\d,[\]]+

And if you only need to extract digits from objects with single field "acks", you could replace \{"acks":"(\d+)"\} with $1.
Demo here.

答案3

得分: 0

只需检查\"acks\":字符以及起始和结束的"字符。

检查{ 似乎不必要。

\"acks\":\"(.+?)\"
String string = "{\"acks\":\"6\"},{\"acks\":\"7\"},{\"acks\":\"8\"}";
Pattern pattern = Pattern.compile("\\\"acks\\\":\\\"(.+?)\\\"");
Matcher matcher = pattern.matcher(string);
List<String> list = new ArrayList<>();
while (matcher.find()) list.add(matcher.group(1));

输出

[6, 7, 8]
英文:

Just check for \"acks\", the : character, and the starting and ending " characters.

The check for { doesn't seem necessary.

\"acks\":\"(.+?)\"
String string = "{\"acks\":\"6\"},{\"acks\":\"7\"},{\"acks\":\"8\"}";
Pattern pattern = Pattern.compile("\\\"acks\\\":\\\"(.+?)\\\"");
Matcher matcher = pattern.matcher(string);
List<String> list = new ArrayList<>();
while (matcher.find()) list.add(matcher.group(1));

Output

[6, 7, 8]

答案4

得分: -1

这应该适用于描述的情况:

System.out.println("[{\"acks\":\"6\"},{\"acks\":\"17\"},{\"acks\":\"888\"}]".replaceAll("(\\{\"acks\":)*(\"})*", ""));

结果:[6,17,888]

我可以看到有一些评论说,与“acks”键不同,可能是任何符号,因此我建议使用以下正则表达式:(\{.*?:\"\d*\"\},)* 参见 https://regex101.com/r/kfAYsQ/1

System.out.println("[{\"key1\":\"6\"},{\"key2\":\"17\"},{\"key3\":\"888\"}]".replaceAll("(\\{.*?:\"\d*\"\},)*", ""));

输入:[{"key1":"6"},{"key2":"17"},{"key3":"888"}]

结果:[6,17,888]

英文:

This should work for the described case:

System.out.println("[{\"acks\":\"6\"},{\"acks\":\"17\"},{\"acks\":\"888\"}]".replaceAll("(\\{\"acks\":\")*(\"})*", ""));

Result: [6,17,888]

I can see that there are some comments that instead of "acks" key might be any symbols, hence I would suggest to use next regexp (\{.*?:\")*(\"})* see https://regex101.com/r/kfAYsQ/1

System.out.println("[{\"key1\":\"6\"},{\"key2\":\"17\"},{\"key3\":\"888\"}]".replaceAll("(\\{.*?:\")*(\"})*", ""));

input: [{\"key1\":\"6\"},{\"key2\":\"17\"},{\"key3\":\"888\"}]

result: [6,17,888]

huangapple
  • 本文由 发表于 2023年6月30日 00:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定