英文:
How to split a String with two delimiters and keep only one of of them?
问题
我想在标点符号和空白字符处拆分字符串,但保留标点符号。例如
String example = "How are you? I am fine!"
我希望得到的结果是
["How","are","you","?","I","am","fine","!"]
但实际上我得到的是
["how"," ","are"," ","you"," ","?"," ","i"," ","am"," ","fine"," ","!"]。
我使用的是 example.toLowerCase().trim().split("(?<=\\b|[^\\p{L}])");
英文:
I want to split a String in punctuation marks and white spaces, but keep the punctuation marks. E.x
String example = "How are you? I am fine!"
I want to have as a result
["How","are","you","?","I","am","fine","!"]
but instead I get
["how"," ","are"," ","you"," ","?"," ","i"," ","am"," ","fine"," ","!"].
what I used was example.toLowerCase().trim().split("(?<=\\b|[^\\p{L}])");
答案1
得分: 2
为什么要使用 toLowerCase()
?这已经影响了您期望的结果。还有为什么对整个字符串使用 trim()
?
使用单个 split
调用可能不太简单。
另一种方法是仅筛选掉不需要的条目:
String example = "How are you? I am fine!";
Pattern pattern = Pattern.compile("\\b");
String[] result = pattern.splitAsStream(example)
.filter(Predicate.not(String::isBlank))
.toArray(String[]::new);
System.out.println(Arrays.toString(result));
输出:
[How, are, you, ? , I, am, fine, !]
针对您希望输出 [How,are,you,?,I,am,fine,!]
的评论,只需不要使用 Arrays.toString
,而是手动构建字符串。数组不包含任何空格。
System.out.println("[" + String.join(",", result) + "]");
英文:
Why are you doing toLowerCase()
? This already messes up your expected result. And why the trim()
on the full string?
Doing this with a single split
call is probably not too simple.
An alternative would be to just filter out the unwanted entries:
String example = "How are you? I am fine!";
Pattern pattern = Pattern.compile("\\b");
String[] result = pattern.splitAsStream(example)
.filter(Predicate.not(String::isBlank))
.toArray(String[]::new);
System.out.println(Arrays.toString(result));
Output:
[How, are, you, ? , I, am, fine, !]
Reacting to your comment of wanting [How,are,you,?,I,am,fine,!]
as output; simply dont print with Arrays.toString
but build the string yourself manually. The array does not contain any whitespaces.
System.out.println("[" + String.join(",", result) + "]");
答案2
得分: 1
您可以按照以下方式操作:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String example = "How are you? I am fine!";
String[] arr = example.split("\\s+|\\b(?=\\p{Punct})");
System.out.println(Arrays.toString(arr));
}
}
**输出:**
[How, are, you, ?, I, am, fine, !]
**正则表达式解释:**
1. `\\s+` 指定空格
2. `\\b` 指定单词边界
3. `(?=\\p{Punct})` 指定标点的正向先行断言。
4. `|` 指定选择(`或`)
英文:
You can do it as follows:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String example = "How are you? I am fine!";
String[] arr = example.split("\\s+|\\b(?=\\p{Punct})");
System.out.println(Arrays.toString(arr));
}
}
Output:
[How, are, you, ?, I, am, fine, !]
Explanation of the regex:
\\s+
specifies the space\\b
specifies the word boundary(?=\\p{Punct})
specifies the positive look ahead for punctuation.|
specifies the alternation (OR
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论