[英]Regex splitting resulting in an extra space, using \\s but not getting rid of it
我正在嘗試使用正則表達式解析字符串,該字符串具有開頭括號和結尾括號,在它們之間列出了小英文字母,用逗號分隔。 每個逗號后跟一個空格。 像這樣
import java.util.Arrays;
import java.util.Set;
import java.util.HashSet;
import java.io.*;
public class StringLetters {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split("\\s*(\\{|,|\\}|\\s)\\s*");
for (int i = 0; i < temp.length; i++) {
System.out.println("temp[" + i + "] ===>" + temp[i]);
}
Set<String> set = new HashSet<>();
for (String a : temp) {
set.add(a);
}
System.out.println(set.size());
} catch (IOException ioe) {
}
}
}
當我將 {a, b, c} 作為輸入傳遞時
$ java StringLetters
{a, b, c}
temp[0] ===>
temp[1] ===>a
temp[2] ===>b
temp[3] ===>c
4
其他輸入可以是 {}, {s, h, a, n, o, n, o}, {h, e, a, l, h, t}...等
所以當我在做
$ java StringLetters
{}
0
這是正確的,對於空字符串,我應該得到這個 0。
臨時數組中第 0 位的空字符串不是我想要的,要擺脫我在 '(\{|,|\}|\s)' 中使用 \s 的情況,但這對我沒有幫助!!
如果您只需要輸入中的單個字母,我會使用與拆分不同的方法 go 。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws ParseException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Pattern pattern = Pattern.compile("\\p{Alpha}");
Matcher matcher = pattern.matcher(br.readLine());
Set<String> set = new HashSet<>();
while (matcher.find()) {
System.out.println(matcher.group());
set.add(matcher.group());
}
System.out.println(set.size());
}
}
示例運行:
{a, b, c}
a
b
c
3
另一個示例運行:
{}
0
注意: \p{Alpha}
代表單個字母,可以替換為[A-Za-Z]
。 在此處了解有關這些模式的更多信息。 您還想查看Java 正則表達式教程。
如果你想堅持自己的方式(即拆分字符串),你可以這樣做:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) throws ParseException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split("\\s*(\\{|,|\\}|\\s)\\s*");
for (int i = 1; i < temp.length; i++) {// Start with index 1
System.out.println("temp[" + i + "] ===>" + temp[i]);
}
Set<String> set = new HashSet<>();
for (String a : temp) {
if (!a.isBlank() && !a.isEmpty()) {// Check if the string is not empty or blank
set.add(a);
}
}
System.out.println(set.size());
}
}
示例運行:
{a, b, c}
temp[1] ===>a
temp[2] ===>b
temp[3] ===>c
3
我在代碼中添加了注釋,以便您更容易注意到更改。 需要這些更改的原因是String#split
返回一個最小大小為1
的數組,例如System.out.println("Hello".split("$").length)
將打印1
。
正則表達式將 { 作為第一個分隔符,給你一個空字符串。 簡單的方法是在創建Set
時過濾數組:
Set<String> set = new HashSet<>();
for(String a : temp){
if (a != null && !a.isEmpty())
set.add(a);
}
在不調用拆分的情況下,上述任務可以按如下方式完成:-
import java.util.Set;
import java.util.HashSet;
import java.io.*;
public class StringLetters {
public static void main(String[] args){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
Set<Character> set = new HashSet<>();
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) != '{' && str.charAt(i) != '}' && str.charAt(i) != ' ' && str.charAt(i) != ','){
set.add(str.charAt(i));
}
}
System.out.println(set.size());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
任務本身很簡單,但我對正則表達式很感興趣,想知道如何使用正則表達式來完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.