java 解析像“0:ADD(10)”这样的字符串
我有一个像这样的字符串数组 {"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用如下正则表达式:
从括号之间提取数字。有关 java regex 的更多信息可以在此处和此处
注意:由于java默认在开头和结尾分别添加
^
和$
,所以我们必须在前面和结尾添加.*
在我们想要匹配的模式之后。印刷:
You can use a regular expression like this:
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.Prints:
你可以尝试
You can try
可能有点矫枉过正,但这应该有效
Might be a bit of overkill, but this should work
使用番石榴:
Using guava:
这一行的工作如下:
this single line does the work: