简体   繁体   English

有条件结束正则表达式

[英]Ending a Regex on a Condition

I am looking for a regex to split a string containing (). 我正在寻找一个正则表达式来拆分包含()的字符串。

This is what I have 这就是我所拥有的

String regex = "(?=\\()|()"

The first part is correct so that it starts with the "(". I need the second part to end on the ")", but if there is a number after the ")" it need to end on the number. 第一部分是正确的,因此它以“(”开头。我需要第二部分以“)”结尾,但是如果“)”之后有数字,则需要以数字结尾。

example Fe2(CH3)2(CH2O)(CH3) 示例Fe2(CH3)2(CH2O)(CH3)

This needs to split into 这需要分成

Fe2 FE2

(CH3)2 (CH3)2

(CH2O) (CH2O)

(CH3) (CH 3)

The number after the ")" can be either a single or double digit. “)”后面的数字可以是一位或两位数。

I also needs to split Fe(C5H5)2O4(CH3) for example into 我还需要将Fe(C5H5)2O4(CH3)拆分为例如

Fe

(C5H5)2 (C5H5)2

O4 O4

(CH3) (CH 3)

Updated, the new logic: match either some letter chars or something parenthesized, followed by optional digits. 更新后的新逻辑:匹配某些字母字符或带括号的内容,后跟可选数字。

final String f = "Fe2(CH3)2O4(CH2O)2(CH3)";
final Matcher m = Pattern.compile("(\\p{L}+|\\(.*?\\))\\d*").matcher(f);
while (m.find()) System.out.println(m.group());

Edit 编辑

Hope this works for you :) 希望这对你有用:)

String s="Fe2(CH3)2(CH2O)(CH3)23O4(CH3)";
String[] array=s.split("(?=\\()|(?<=\\)[0-9]{1,2}+)");
System.out.println(Arrays.toString(array));
//out: [Fe2, (CH3)2, (CH2O), (CH3)23, O4, (CH3)]

I don't know about java-regex specific things but this worked for me in .net: 我不知道有关Java-regex的特定信息,但这在.net中对我有用:
(^[A-Za-z0-9]+)|(\\([A-Za-z0-9]+\\)[A-Za-z0-9]{0,2})
(unescaped, put additional \\ where needed...) (未转义,在需要的地方附加\\ …)

EDIT: now I have read about the stuff you're doing. 编辑:现在,我已经阅读了有关您正在做的事情。 My regex WON'T work for SPLITTING, rather you should call match on it. 我的正则表达式不能用于拆分,而应该在其上调用match And every match will be one piece for you. 每场比赛对您来说都是一件。

And if this doesn't exactly do what you want, please specify more inputs and desired outputs (especially ones that this one doesn't cover), and I'm more than happy to help... 如果这不能完全满足您的要求,请指定更多的输入和所需的输出(尤其是该输入不能涵盖的输出),我非常乐于提供帮助...

Looks like i found a soln which works for all cases: 看来我找到了一种适用于所有情况的解决方案:

Enter your regex: \(.*?\)[0-9]{0,2}|[\w]+
Enter input string to search: Fe2(CH3)2O4(CH2O)(CH3)
I found the text "Fe2" starting at index 0 and ending at index 3.
I found the text "(CH3)2" starting at index 3 and ending at index 9.
I found the text "O4" starting at index 9 and ending at index 11.
I found the text "(CH2O)" starting at index 11 and ending at index 17.
I found the text "(CH3)" starting at index 17 and ending at index 22.

Here is the regex to be used in the code: 这是在代码中使用的正则表达式:

String regEx = "\\(.*?\\)[0-9]{0,2}|[\\w]+";

In short it looks for strings starting with '(' and ending with ')' and numbers. 简而言之,它查找以'('开始,以')'结尾的字符串和数字。 If thats not there then it looks for strings like 'Fe2' , 'O4' etc. 如果那还不存在,那么它将查找类似“ Fe2”,“ O4”等的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM