簡體   English   中英

Java中的正則表達式“[\\p{Punct}\\s]+”有什么作用?

[英]What does the regular expression "[\\p{Punct}\\s]+" do in Java?

我目前是 Java 的新手,希望對理解這行代碼有所幫助,這個正則表達式的作用/含義是什么。

String[] s = l.split("[\\p{Punct}\\s]+");

拆分 function 將根據您在給定模式中指定的分隔字符返回一個字符串數組。

根據 Java Pattern 文檔: Class Pattern ,您使用的相關分隔符號是:

  • \p{Punct} 標點符號:之一,,"#$%&'()*+.-:/;?<=>?@[]^_`{|}~
  • \s 空白字符:[ \t\n\x0B\f\r]

因此,字符串數組將由每次在整個字符串中找到上述字符之一時分隔的所有字符串組成。

這是一個可以用來測試它的示例:

import java.util.*;

public class TestSplit {
    public static void main(String args[]) {
        String myStringTest = "test,of#the@split&separated by(space)and+punctuations";
        List<String> list = Arrays.asList(myStringTest.split("[\\p{Punct}\\s]+"));
        System.out.println(list);
    }
}

這是您得到的 output:

[test, of, the, split, separated, by, space, and, punctuations]

暫無
暫無

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

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