[英]regular expression for string split in java
I have string in the following form: 我有以下形式的字符串:
HOME(SPADE0) HOME(HEART0) HOME(CLUB0) BOTTOMCOL(CLUBA) ON(HEART2 CLUBA)
I would lilke to split it into 我会喜欢把它分成
HOME(SPADE0)
HOME(HEART0)
HOME(CLUB0)
BOTTOMCOL(CLUBA)
ON(HEART2 CLUBA)
splitting at space splits the last token also, which I don't want . 在空间上拆分还会拆分最后一个令牌,我不希望这样做。 What can be a suitable regular expression for it?
什么是合适的正则表达式?
Thanks in advance! 提前致谢!
EDIT 编辑
String[] tokens = line.split("[)]\\s+");
Better split by matching the content instead of the delimiters: 通过匹配内容而不是分隔符,可以更好地进行拆分:
final Matcher m = Pattern.compile("\\w+\\(.*?\\)").matcher(input);
final List<String> matches = new ArrayList<>();
while (m.find()) matches.add(m.group());
Try this regex (Using Negative look-ahead
): - 尝试使用此正则表达式(使用
Negative look-ahead
):-
String[] arr = str.split("\\s+(?![^(]*\\))");
System.out.println(Arrays.toString(arr));
It will only split on space, which is not in between (
and )
. 它将仅在空间上分割,该空间不在
(
和)
之间。
OUTPUT : - 输出 :-
[HOME(SPADE0), HOME(HEART0), HOME(CLUB0), BOTTOMCOL(CLUBA), ON(HEART2 CLUBA)]
Explanation: - 说明:-
\\s+ // split on space (one or more)
(?! // Negative look ahead (Not followed by)
[^(]* // Anything except `(` (0 or more)
\\) // Ending with `)`
) // End
So, if your space is between, (
and )
as in (HEllo World)
. 因此,如果您的空间介于
(
和)
之间,则与(HEllo World)
。
It will not match the above regex. 它与上面的正则表达式不匹配。 Because the space in there is followed by : -
因为那里的空格后面是:-
[^(]* // Any string not containing `(` - World
\\) // Ending with `)`
Note that, although this will solve your problem with split
. 请注意,尽管这将解决
split
问题。 But ideally, this should be done with Pattern
and Matcher
. 但理想情况下,应使用
Pattern
和Matcher
完成此操作。 As in @Marko's answer. 就像@Marko的答案一样。
这应该工作:
Pattern ptrn = Pattern.compile("\\w+\\(.+?\\)");
Why not just split on the ")" and then append it to all found tokens? 为什么不只是在“)”上分割,然后将其附加到所有找到的标记上呢?
String [] results = str.split( ")" );
String token1 = results[0].trim() + ")"; // the trim is to remove leading spaces
This is assuming that all your data matches the presented format. 假设您的所有数据都与显示的格式匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.