简体   繁体   中英

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 ; 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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