Java正则表达式分割WebService返回字符串至不同字段
Got it, since your WebService returns a string with a consistent fixed format (only the actual values change), we can split it reliably into your target fields. Let's walk through two practical approaches using Java, since your example uses String variables:
Approach 1: Basic String Splitting (Simple & Direct)
This works great if the format is strictly consistent (e.g., comma separators, postal code always followed by city and a parenthetical suffix).
Step-by-Step Code:
String wsResult = "SONACOM RC, RUE DES ETOILES N. 20, 75250 PARIS (MI)"; // 1. Split the string by commas and trim whitespace from each segment String[] segments = wsResult.split(","); String name = segments[0].trim(); String address = segments[1].trim(); // 2. Process the third segment to extract postal code and city String locationSegment = segments[2].trim(); // Split at the first space to get the postal code int firstSpace = locationSegment.indexOf(" "); String postalCode = locationSegment.substring(0, firstSpace).trim(); // Extract city by cutting off the parenthetical suffix int parenthesisStart = locationSegment.indexOf("("); String city = locationSegment.substring(firstSpace + 1, parenthesisStart).trim(); // Verify the results System.out.println("Name: " + name); // Output: SONACOM RC System.out.println("Address: " + address); // Output: RUE DES ETOILES N. 20 System.out.println("Postal Code: " + postalCode); // Output: 75250 System.out.println("City: " + city); // Output: PARIS
Approach 2: Regular Expressions (Robust & Flexible)
Use regex if you need to handle minor format variations (e.g., extra spaces around commas, varying parenthetical suffixes). The pattern captures exactly the fields we need.
Step-by-Step Code:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class WsStringParser { public static void main(String[] args) { String wsResult = "SONACOM RC, RUE DES ETOILES N. 20, 75250 PARIS (MI)"; // Regex pattern with capture groups for each field String regexPattern = "^(.*?),\\s*(.*?),\\s*(\\d+)\\s*(.*?)\\s*\\(.*?\\)$"; Pattern pattern = Pattern.compile(regexPattern); Matcher matcher = pattern.matcher(wsResult); if (matcher.find()) { String name = matcher.group(1).trim(); String address = matcher.group(2).trim(); String postalCode = matcher.group(3).trim(); String city = matcher.group(4).trim(); // Verify results System.out.println("Name: " + name); System.out.println("Address: " + address); System.out.println("Postal Code: " + postalCode); System.out.println("City: " + city); } else { // Handle unexpected format (in case WS returns invalid data) System.err.println("Error: Unexpected format from WebService"); } } }
Regex Pattern Breakdown:
^(.*?): Captures the name (everything up to the first comma),\s*(.*?): Captures the address (between the first and second commas, ignoring extra spaces),\s*(\d+): Captures the numeric postal code\s*(.*?)\s*\(.*?\)$: Captures the city (between the postal code and the parenthetical suffix)
Optional Adjustment:
If the parenthetical suffix might be missing, modify the regex to make that part optional:
^(.*?),\s*(.*?),\s*(\d+)\s*(.*?)(?:\s*\(.*?\))?$
Key Notes:
- Test with multiple sample outputs from your WebService to confirm the format stays consistent.
- If your postal code format changes (e.g., non-numeric characters), update the
\d+part of the regex to match the new pattern.
内容的提问来源于stack exchange,提问作者KhaledZero

