简体   繁体   English

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

[英]Capture text between 2 special characters using regex

I'm trying to determine the best regular expression to capture text from the suite values in the following example strings: 我正在尝试确定最佳正则表达式,以从以下示例字符串中的套件值中捕获文本:

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

For example I need to capture the following from the above text: 例如,我需要从上面的文本中捕获以下内容:

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

Basically I need to capture all the text between Suite: and ; 基本上,我需要捕获Suite:;之间的所有文本; excluding the first whitespace. 排除第一个空格。

I am trying to do this in Java and can't come up with a regular expression that would work for multiple scenarios. 我正在尝试使用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);
}

Prints: 印刷品:

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

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

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