4

Javaで区切られたスペースで単語を分割する必要があるため.split、以下に示すように、関数を使用してそれを達成しました

String keyword = "apple mango ";
String keywords [] = keyword .split(" ");

上記のコードは正常に機能していますが、キーワードに「ジャック フルーツ」「アイス クリーム」などのキーワードが含まれ、以下に示すように二重引用符が付けられることがあります。

String keyword = "apple mango \"jack fruit\" \"ice cream\"";

この場合、キーワード配列でapplemangojack fruitice creamのような 4 つの単語を取得する必要があります

誰かこれの解決策を教えてください

4

5 に答える 5

4
List<String> parts = new ArrayList<>();
String keyword = "apple mango \"jack fruit\" \"ice cream\"";

// first use a matcher to grab the quoted terms
Pattern p = Pattern.compile("\"(.*?)\"");      
Matcher m = p.matcher(keyword);
while (m.find()) {
    parts.add(m.group(1));
}

// then remove all quoted terms (quotes included)
keyword = keyword.replaceAll("\".*?\"", "")
                 .trim();

// finally split the remaining keywords on whitespace
if (keyword.replaceAll("\\s", "").length() > 0) {
    Collections.addAll(parts, keyword.split("\\s+"));
}

for (String part : parts) {
    System.out.println(part);
}

出力:

jack fruit
ice cream
apple
mango
于 2016-12-01T14:32:11.377 に答える
3

正規表現と 2 つのキャプチャ グループ (パターンごとに 1 つ) を使用してそれを行います。私は他の方法を知りません。

    String keyword = "apple mango \"jack fruit\" \"ice cream\"";
    Pattern p = Pattern.compile("\"?(\\w+\\W+\\w+)\"|(\\w+)");      
    Matcher m = p.matcher(keyword);
    while (m.find()) {
        String word = m.group(1) == null ? m.group(2) : m.group(1);
        System.out.println(word);
    }
于 2016-12-01T14:22:12.757 に答える
0

このソリューションは機能しますが、パフォーマンス/リソースにとって最適ではないと確信しています。単語が 2 つ以上の果物がある場合にも機能します。私のコードを自由に編集または最適化してください。

public static void main(String[] args) {
        String keyword = "apple mango \"jack fruit\" \"ice cream\" \"one two three\"";
        String[] split = custom_split(keyword);
        for (String s : split) {
            System.out.println(s);
        }
    }

    private static String[] custom_split(String keyword) {
        String[] split = keyword.split(" ");
        ArrayList<String> list = new ArrayList<>();
        StringBuilder temp = new StringBuilder();
        boolean multiple = false;
        for (String s : split) {
            if (s.startsWith("\"")) {
                multiple = true;
                s = s.replaceAll("\"", "");
                temp.append(s);
                continue;
            }
            if (s.endsWith("\"")) {
                multiple = false;
                s = s.replaceAll("\"", "");
                temp.append(" ").append(s);
                list.add(temp.toString());
                temp = new StringBuilder();
                continue;
            }
            if (multiple) {
                temp.append(" ").append(s);
            } else {
                list.add(s);
            }
        }
        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
于 2016-12-01T14:15:49.907 に答える
0

でそれを行うことはできませんString.split()。次のように、ターゲット トークンの正規表現を考え出し、マッチャーを介してそれらを収集する必要があります。

    final Pattern token = Pattern.compile( "[^\"\\s]+|\"[^\"]*\"" );

    List<String> tokens = new ArrayList<>();
    Matcher m = token.matcher( "apple mango \"jack fruit\" \"ice cream\"" );
    while( m.find() )
        tokens.add( m.group() );
于 2016-12-01T14:21:05.473 に答える