首页 > 解决方案 > 如何用引号,站点运算符和非引号分割字符串?

问题描述

我收到这样的用户请求

site:www.example.com \"hello world\" \"hi abc\" where are you

我想从这个字符串中提取并保存 url 然后从上面的字符串中删除它,它应该看起来像这样"hello world" "hi abc" where are you现在将剩余的字符串拆分为两个字符串数组

String str1 = {hello world, hi abc};
String str2 = {where, are, you};

我怎么能在java中做到这一点?用户查询可以是任何顺序。各种例子:

 "hi" excitement site:www.example.com \"hello world\" \"hi abc\" where are you "amazing"   
OR
    Hello World friends
OR
 Greeting is an "act of communication" human beings "intentionally"  

标签: javastringparsing

解决方案


这是一个非常具体的问题,下面的逻辑可能对您有所帮助。我建议您在使用实际数据进行测试时完善这一点。

public static void main(String[] args) {
    String test1 = "site:www.example.com \"hello world\" \"hi abc\" where are you";
    String regex = "\\b(https?|ftp|file|site):[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
    String[] info = test1.split("\"");

    //read url
    String url;
    if (info.length > 0 && info[0].trim().matches(regex))
        url = info[0].trim();
    else
        throw new RuntimeException("Not a valid input");

    // read str1
    String[] info1 = Arrays.copyOfRange(info, 1, info.length - 1);
    String str1 = mkString(info1, ",");

    //read str2
    String[] info2 = info[info.length - 1].trim().split("\\s");
    String str2 = mkString(info2, ",");


    System.out.println("URL: " + url + " STR1: " + str1 + " STR2: " + str2);

}

// returns a delimited and curly parentheses {} enclosed string
public static String mkString(String[] input, String delimeter) {
    String result = "{";
    for (int i = 0; i < input.length - 1; i++) {
        if (input[i].trim().length() > 0) {
            result += (input[i] + delimeter);
        }
    }
    result += (input[input.length - 1] + "}");
    return result;
}

推荐阅读