簡體   English   中英

如何拆分字符串,只保留某些分隔符?

[英]How to split a string, keeping only certain delimiters?

我有一個類似於如何拆分字符串的問題,但也保留了分隔符? 我如何使用正則表達式拆分字符串,保留某些類型的分隔符,而不是其他類型的分隔符? 具體來說,我想保留非空白分隔符,但不保留空白分隔符。

為了使這個具體:

"a;b c"        | ["a", ";", "b", "c"]
"a; ; bb c ;d" | ["a", ";", ";", "bb", "c", ";", "d"]

這可以用正則表達式干凈地完成,如果是這樣的話怎么樣?

現在我正在解決這個問題,分別保留角色,然后再另一個。 如果正則表達式不能這樣做,或者不能干凈利落,我可以堅持這種方法:

Arrays.stream(input.split("((?<=;)|(?=;))"))
        .flatMap(s -> Arrays.stream(s.split("\\s+")))
        .filter(s -> !s.isEmpty())
        .toArray(String[]::new); // In practice, I would generally use .collect(Collectors.toList()) instead

我建議捕捉你想要的東西,而不是使用這個簡單的模式分裂

([^; ]+|;)

演示

你可以這樣做:

System.out.println(String.join("-", "a; ; b c ;d".split("(?!\\G) *(?=;)|(?<=;) *| +")));

細節:

(?!\\G)  # not contiguous to a previous match and not at the start of the string
[ ]*     # optional spaces
(?=;)    # followed by a ;
|    # OR
(?<=;)   # preceded by a ;
[ ]*     # optional spaces
|    # OR
[ ]+     # several spaces 

隨意將文字空間更改為\\\\s 要避免空項(當字符串以空格開頭時,在結果數組的開頭) ,您需要首先修剪字符串。

顯然,沒有分裂的約束,@ alphabravo方式是最簡單的。

我找到了一個有效的正則表達式:

(\\s+)|((?<=;)(?=\\S)|(?<=\\S)(?=;))
public static void main(String argss[]){
    System.out.println(Arrays.toString("a; ; b c ;d"
        .split("(\\s+)|((?<=;)(?=\\S)|(?<=\\S)(?=;))")));
}

將打印出來:

[a, ;, ;, b, c, ;, d]

您想要在空格上或在字母和非字母之間拆分:

str.split("\\s+|(?<=\\w)(?=\\W)|(?<=\\W)(?=\\w)");

在實現Java不支持將捕獲的拆分字符添加到
拆分數組元素,以為我會嘗試沒有它的拆分解決方案
能力。

基本上只有4個排列涉及空格和結腸。
最后,只有空白。

這是正則表達式。

Raw: \\s+(?=;)|(?<=;)\\s+|(?<!\\s)(?=;)|(?<=;)(?!\\s)|\\s+

弦樂: "\\\\s+(?=;)|(?<=;)\\\\s+|(?<!\\\\s)(?=;)|(?<=;)(?!\\\\s)|\\\\s+"

擴展的正則表達式與排列的解釋。
祝好運!

    \s+                  # Required, suck up wsp before ;
    (?= ; )              # ;

 |                     # or,

    (?<= ; )             # ;
    \s+                  # Required, suck up wsp after ;

 |                     # or,

    (?<! \s )            # No wsp before ;
    (?= ; )              # ;

 |                     # or,

    (?<= ; )             # ;
    (?! \s )             # No wsp after ;

 |                     # or,

    \s+                  # Required wsp

編輯

要停止在BOS上的空白分割,請使用此正則表達式。

Raw: \\s+(?=;)|(?<=;)\\s+|(?<!\\s)(?=;)|(?<=;)(?!\\s)|(?<!^)(?<!\\s)\\s+

弦樂: "\\\\s+(?=;)|(?<=;)\\\\s+|(?<!\\\\s)(?=;)|(?<=;)(?!\\\\s)|(?<!^)(?<!\\\\s)\\\\s+"

解釋:

    \s+                  # Required, suck up wsp before ;
    (?= ; )              # ;

 |                     # or,

    (?<= ; )             # ;
    \s+                  # Required, suck up wsp after ;

 |                     # or,

    (?<! \s )            # No wsp before ;
    (?= ; )              # ;

 |                     # or,

    (?<= ; )             # ;
    (?! \s )             # No wsp after ;

 |                     # or,

    (?<! ^ )             # No split of wsp at BOS   
    (?<! \s )
    \s+                  # Required wsp

借用@CasimiretHippolyte \\G技巧你可能想分開

\\s+|(?!\\G)()

注意:未指定分隔符。

更新

基於避免在第一個空間上拆分:

(?m)(?<!^|\\s)(\\s+|)(?!$)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM