简体   繁体   English

如何将字符串拆分成组?

[英]How can I split a string into groups?

I'm trying to work out how to split a string into groups. 我正在尝试解决如何将字符串拆分成组。 I don't think the split(regex) method will suffice on it's own. 我不认为split(regex)方法就足够了。

I have String complexStatement = "(this && that)||(these&&those)||(me&&you)"; 我有String complexStatement = "(this && that)||(these&&those)||(me&&you)"; and I would like an array out with this kind of form: 我想用这种形式的阵列:

"(this && that)","(these&&those)","(me&&you)""

If I had "(5+3)*(2+5)+(9)" then I'd like to have "(5+3)","(2+5)","(9)". 如果我有"(5+3)*(2+5)+(9)"那么我想要“(5 + 3)”,“(2 + 5)”,“(9)”。
(bonus points if you can somehow keep the join information, eg *,+,|| ) (如果你能以某种方式保留加入信息,奖励积分,例如*,+,||

Is this possible for an arbitrary string input? 这对于任意字符串输入是否可行? I'm playing with a StringTokenizer but I haven't quite gotten to grips with it yet. 我正在玩一个StringTokenizer,但我还没有完全掌握它。

You can use the bellow code: 您可以使用以下代码:

    String str = "(this && that)\",\"(these&&those)\",\"(me&&you)";
    Pattern pattern = Pattern.compile("\\(([^\\)]+)\\)");
    Matcher m = pattern.matcher(str);
    while (m.find()){
        System.out.println(m.group(0));
    }

\\\\(([^\\\\)]+)\\\\) will dig you anything within the parenthesis, look like what you want!: \\\\(([^\\\\)]+)\\\\)会在括号内挖掘任何东西,看起来像你想要的!:

Edit: 编辑:

To capture content between ) and ( just replace the regular expression with \\\\)([^\\\\(]+)\\\\( this one! 捕获之间的内容)(只需用\\\\)([^\\\\(]+)\\\\(替换正则表达式\\\\)([^\\\\(]+)\\\\(这一个!

I think you better implement the parsing instead of depending on any ready-made methods. 我认为你最好实现解析,而不是依赖于任何现成的方法。

Here is my suggestion... I am assuming the format of input will be always like followig 这是我的建议......我假设输入的格式总是像followig

(value1+operator+value2)+operator+(value3+operator+value4)+........

[here operator can be different, and + is just showing concatanation). [这里的运算符可以不同,而+只是显示连接)。

If the above assumptio is true then you can do the following. 如果上述假设为真,那么您可以执行以下操作。

  1. Use a stack 使用堆栈
  2. While reading the original string push all the characters into the stack 在读取原始字符串时将所有字符推入堆栈
  3. now popup one by one from the stack by using following logic a. 现在通过使用以下逻辑a从堆栈中逐个弹出。 if get ) start adding to a string b. 如果get)开始添加到字符串b。 if get ( add to the string and now you get one token. add the token to the array. c. after getting ( skip till the next ). 如果get(添加到字符串,现在你得到一个标记。将标记添加到数组中.c。获取后(跳到下一个)。

NB it's just and pseudo code with primitive thinking. 注意它只是和原始思维的伪代码。

If you want to capture the groups defined only by parentheses at the outermost level, you are outside of the world of regular expressions and will need to parse the input. 如果要捕获由最外层括号定义的组,则您不在正则表达式的世界中,需要解析输入。 StinePike's approach is good; StinePike的做法很好; another one (in messy pseudocode) is as follows: 另一个(凌乱的伪代码)如下:

insides = []
outsides = []
nesting_level = 0
string = ""
while not done_reading_input():
    char = get_next_char()
    if nesting_level > 0 or char not in ['(', ')']:
        string += char
    if char == '('
        if nesting_level == 0:
            outsides.add(string)
            string = ""
        nesting_level += 1
    elif char == ')':
        nesting_level -= 1
        if nesting_level == 0:
            insides.add(string)
            string = ""

If the very first character in your input is a '(', you'll get an extra string in your outsides array, but you can fix that without much trouble. 如果你输入的第一个字符是'(',你会在你的outsides数组中得到一个额外的字符串,但你可以毫不费力地修复它。

If you are interested in nested parentheses then you will not be producing just two arrays as output; 如果您对嵌套括号感兴趣,那么您将不会仅生成两个数组作为输出; you will need a tree. 你需要一棵树。

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

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