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

Capture text between 2 special characters using regex

本文关键字:特殊字符 之间 文本 正则表达式      更新时间:2023-09-26

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

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 中执行此操作,但无法想出适用于多种场景的正则表达式。

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