java 解析像“0:ADD(10)”这样的字符串

发布于 2024-10-08 00:34:42 字数 180 浏览 0 评论 0原文

我有一个像这样的字符串数组 {"0:ADD(10)","1:ADD(20)"} 我如何解析它并从该字符串中获取值 '10','20' ,

场景是:我正在尝试使用 Apache Mina 服务器编写一个客户端和服务器应用程序,当我在客户端和服务器之间发送消息时,我收到的响应为“0:ADD(10)”

i am having a string array like this {"0:ADD(10)","1:ADD(20)"} how can i parse this and get the value '10','20' from this string ,

Scenario is : i am trying to write a client and server app using Apache Mina server , when i sent message between client and server i am getting response as "0:ADD(10)"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

断舍离 2024-10-15 00:34:42

您可以使用如下正则表达式:

\\((\\d+)\\)

从括号之间提取数字。有关 java regex 的更多信息可以在此处此处
注意:由于java默认在开头和结尾分别添加^$,所以我们必须在前面和结尾添加.*在我们想要匹配的模式之后。

String[] str  = new String[]{"0:ADD(10)","1:ADD(20)"};
        Pattern pattern = Pattern.compile("^.*\\((\\d+)\\).*$");
        for (int i = 0; i < str.length; i++)
        {
            Matcher m = pattern.matcher(str[i]);
            System.out.println(m.matches());
            System.out.println(m.group(1));
        }

印刷:

正确

10

正确

20

You can use a regular expression like this:

\\((\\d+)\\)

to extract the number from between the brackets. More information on java regex can be found here and here
Note: Since java adds, by default, the ^ and $ at the beginning and end respectively, we have to add the .* before and after the pattern we want to match.

String[] str  = new String[]{"0:ADD(10)","1:ADD(20)"};
        Pattern pattern = Pattern.compile("^.*\\((\\d+)\\).*$");
        for (int i = 0; i < str.length; i++)
        {
            Matcher m = pattern.matcher(str[i]);
            System.out.println(m.matches());
            System.out.println(m.group(1));
        }

Prints:

true

10

true

20

迷迭香的记忆 2024-10-15 00:34:42
    String[] split = "{\"0:ADD(10)\",\"1:ADD(20)\"}".split( "," ); 
    for ( String e : split ) {
        String arg = e.substring( e.indexOf( '(' )+1, e.indexOf( ')') );
         System.out.println( arg );
     }
    String[] split = "{\"0:ADD(10)\",\"1:ADD(20)\"}".split( "," ); 
    for ( String e : split ) {
        String arg = e.substring( e.indexOf( '(' )+1, e.indexOf( ')') );
         System.out.println( arg );
     }
不知所踪 2024-10-15 00:34:42

你可以尝试

int num = Integer.parseInt("0:ADD(10)".split("[()]")[1]);

You can try

int num = Integer.parseInt("0:ADD(10)".split("[()]")[1]);
赠佳期 2024-10-15 00:34:42

可能有点矫枉过正,但这应该有效

    String input="{\"0:ADD(10)\",\"1:ADD(20)\"}";
    Pattern r= Pattern.compile(":ADD\\(([0-9]*)\\)");
    Matcher m = r.matcher(input);
    while(m.find()){
        System.out.println(m.group(1)); //Result is here
    }

Might be a bit of overkill, but this should work

    String input="{\"0:ADD(10)\",\"1:ADD(20)\"}";
    Pattern r= Pattern.compile(":ADD\\(([0-9]*)\\)");
    Matcher m = r.matcher(input);
    while(m.find()){
        System.out.println(m.group(1)); //Result is here
    }
属性 2024-10-15 00:34:42

使用番石榴

String digits=(digits=CharMatcher.DIGIT.retainFrom("0:ADD(10)")).substring(1,digits.length());

Using guava:

String digits=(digits=CharMatcher.DIGIT.retainFrom("0:ADD(10)")).substring(1,digits.length());
断肠人 2024-10-15 00:34:42

这一行的工作如下:

String[] arr = str.replace("{", "").replace("}", "").replaceAll("\"\\d+:ADD\\((\\d+)\\)\"", "$1").split(",");

this single line does the work:

String[] arr = str.replace("{", "").replace("}", "").replaceAll("\"\\d+:ADD\\((\\d+)\\)\"", "$1").split(",");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文