私は文字列の状態略語を探しています。次に入力文字列の例を示します。
String inputStr = 'Albany, NY + Chicago, IL and IN, NY, OH and WI';
状態の略語に一致させるために使用しているパターンは次のとおりです。
String patternStr = '(^|\\W|\\G)[a-zA-Z]{2}($|\\W)';
私は試合をループしてループ中に非アルファベットを取り除いていますが、私はそれを1回のパスで行うことができるはずです。現在のアプローチは次のとおりです。
Pattern myPattern = Pattern.compile(patternStr);
Matcher myMatcher = myPattern.matcher(inputStr);
Pattern alphasOnly = Pattern.compile('[a-zA-Z]+');
String[] states = new String[]{};
while (myMatcher.find()) {
String rawMatch = inputStr.substring(myMatcher.start(),myMatcher.end());
Matcher alphaMatcher = alphasOnly.matcher(rawMatch);
while (alphaMatcher.find()) {
states.add(rawMatch.substring(alphaMatcher.start(),alphaMatcher.end()));
}
}
System.debug(states);
|DEBUG|(NY, IL, IN, NY, OH, WI)
これはうまくいきますが、冗長でおそらく非効率です。これをJava/Apexで行うには、ワンパスの方法は何ですか?