java中分割字符串的正则表达式
我想将字符串 [AO_12345678, Real Estate]
拆分为 AO_12345678
和 Real Estate
我如何使用正则表达式在 Java 中执行此操作?
我面临的主要问题是避免“[”和“]”
请帮忙
I want to split the string say [AO_12345678, Real Estate]
into AO_12345678
and Real Estate
how can I do this in Java using regex?
main issue m facing is in avoiding "[" and "]"
please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它真的必须是正则表达式吗?
如果不:
Does it really have to be regex?
if not:
我会采取务实的方式:
如果字符串总是被“[]”包围,您可以直接将其子串化而不进行检查。
I'd go the pragmatic way:
If the string is always surrounded with '[]' you can just substring it without checking.
假设所有输入的格式一致,一种简单的方法是完全忽略正则表达式并将其拆分。类似下面的内容是可行的:
当然,您可以根据需要进行定制 - 例如,您可能需要在移除大括号之前检查它们是否存在。或者您可能希望保留逗号之前的任何空格作为第一个字符串的一部分。然而,这应该为您提供根据您的具体要求进行修改的基础。
在像这样的简单情况下,我更喜欢上面这样的代码,而不是提取两个字符串的正则表达式 - 我认为前者更清晰!
One easy way, assuming the format of all your inputs is consistent, is to ignore regex altogether and just split it. Something like the following would work:
Of course you can tailor this as you wish - you might want to check whether the braces are present before removing them, for example. Or you might want to keep any spaces before the comma as part of the first string. This should give you a basis to modify to your specific requirements however.
And in a simple case like this I'd much prefer code like the above to a regex that extracted the two strings - I consider the former much clearer!
您还可以使用 StringTokenizer。代码如下:
s1=AO_12345678
s1=Real Estate
有关 StringTokenizer 的信息,请参阅 javadocs
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
you can also use StringTokenizer. Here is the code:
s1=AO_12345678
s1=Real Estate
Refer to javadocs for reading about StringTokenizer
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
使用正则表达式 (RE) 捕获组的另一个选项:
如果考虑速度/内存,可以将 RE 优化为(使用占有量词而不是贪婪量词)
"\\[([^,]*+),\\s*+([^\\]]*+)\\]"
Another option using regular expressions (RE) capturing groups:
If speed/memory is a concern, the RE can be optimized to (using Possessive quantifiers instead of Greedy ones)
"\\[([^,]*+),\\s*+([^\\]]*+)\\]"