简体   繁体   English

使用正则表达式匹配重复序列

[英]Using regular expressions to match repeating sequence

I am trying to convert a TCL keyed list to a Java HashMap and need to use regular expressions to break pieces of the text out. 我正在尝试将TCL键控列表转换为Java HashMap并且需要使用正则表达式将文本片段分开。 A sample of the text I am using is: 我正在使用的文本示例是:

{ID {{NAME X } {HUB 0   } {NUM 14226188  }}} {SRCID {{NAME XY } {HUB 0   } {NUM 14226136  }}} {TYPE DATA }

Here there are three distinct groups ID, SRCID, and TYPE. 这里有三个不同的组ID,SRCID和TYPE。 I have tried using the regexp {.*?} but the first two groups get broken up. 我已经尝试过使用正则表达式{.*?}但是前两组分解了。 If I use just {.*} then the whole string is matched as a unit, not three. 如果仅使用{.*}则整个字符串将作为一个单元而不是三个匹配。 Any suggestions of what I should try next? 关于下一步应该尝试的任何建议?

Here is sample code with regex to meet your requirement : 这是带有正则表达式的示例代码,可以满足您的要求:

public class RegexTester {
public static void main(String[] args) throws Exception {
    String data = "{ID {{NAME X } {HUB 0   } {NUM 14226188  }}} {SRCID {{NAME XY } {HUB 0   } {NUM 14226136  }}} {TYPE DATA }";

    Pattern pattern = Pattern.compile("(\\{\\bID.+\\})\\s*(\\{SRCID.+\\})\\s*(\\{TYPE DATA.+\\})");
    Matcher matcher = pattern.matcher(data);

    while (matcher.find()) {

        System.out.println(matcher.group(1)); // Group - ID
        System.out.println(matcher.group(2)); // Group - SRCID
        System.out.println(matcher.group(3)); // Group - TYPE DATA
    }
}

Output : 输出:

{ID {{NAME X } {HUB 0 } {NUM 14226188 }}} {ID {{NAME X} {HUB 0} {NUM 14226188}}}
{SRCID {{NAME XY } {HUB 0 } {NUM 14226136 }}} {SRCID {{NAME XY} {HUB 0} {NUM 14226136}}}
{TYPE DATA } {TYPE DATA}

Hope this helps. 希望这可以帮助。

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

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