使用正则表达式捕获2个特殊字符之间的文本

时间:2015-01-28 21:29:07

标签: java javascript regex

我正在尝试确定最佳正则表达式,以便从以下示例字符串中的套件值中捕获文本:

Floor: 05; Suite: CPO 5th Floor; Abbrv: CAN-ON-Toronto-CPO5; M9V 1H5
Floor: 05; Suite: CPO 5th Floor; Abbrv: CAN-ON-Toronto-CPO5; M9V 1H5
Floor: 04; Suite: CPO 4th Floor; Abbrv: CAN-ON-Toronto-CPO4; M9V 1H5
Floor: 2; Suite: SOC 2nd Floor; Abbrv: CAN-ON-Scarborough-SOC2; M1H 2X3

例如,我需要从上面的文本中捕获以下内容:

CPO 5th Floor
CPO 5th Floor
CPO 4th Floor
SOC 2nd Floor

基本上我需要捕获Suite:;之间的所有文字,不包括第一个空格。

我试图在Java中执行此操作,但无法提供适用于多种方案的正则表达式。

1 个答案:

答案 0 :(得分:2)

String str = " Floor: 05; Suite: CPO 5th Floor; Abbrv: CAN-ON-Toronto-CPO5; M9V 1H5 "
           + " Floor: 05; Suite: CPO 5th Floor; Abbrv: CAN-ON-Toronto-CPO5; M9V 1H5 "
           + " Floor: 04; Suite: CPO 4th Floor; Abbrv: CAN-ON-Toronto-CPO4; M9V 1H5 "
           + " Floor: 2; Suite: SOC 2nd Floor; Abbrv: CAN-ON-Scarborough-SOC2; M1H 2X3";

// Pattern: Suite:[ ]*([^;]*);
// Which means:
//   Suite:      - first the string "Suite:"
//   [ ]*        - followed by any amount of whitespace 
//   ([^;]*)     - then a capture group that will contain any
//                 amount of characters except ";"
//   ;           - then the character ;
Pattern pattern = Pattern.compile("Suite:[ ]*([^;]*);");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
    String match = matcher.group(1); // first capture group
    System.out.println(match);
}

打印:

CPO 5th Floor
CPO 5th Floor
CPO 4th Floor
SOC 2nd Floor