java - 如何用引号,站点运算符和非引号分割字符串?
问题描述
我收到这样的用户请求
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"
解决方案
这是一个非常具体的问题,下面的逻辑可能对您有所帮助。我建议您在使用实际数据进行测试时完善这一点。
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;
}
推荐阅读
- css - 在具有垂直对齐属性的元素的对齐中适用的规则是什么?
- arrays - 从一个函数返回一个嵌套数组,作为参数传递给另一个函数
- python - 如何从字符串中计算重复的字符
- colors - if/else 的 Guppy 颜色变化问题。如何解决?
- python - Django 模板。整数迭代的最佳方法
- r - R:根据条件将函数应用于两个不同的表(空间连接)
- python - 如何在映射多个 Foursquare 结果且 API 结果为空时数据框为空时在 Python 中抑制 KeyError
- python - 绘制熊猫数据框
- jenkins - Sonarqube 扫描获取整个代码或仅获取哈希值
- javascript - Ion.RangeSlider。解析值的问题