Java正則表達(dá)式value.split(“ \\\。”),“反斜杠點(diǎn)”是否按字符分隔?
我的猜測(cè)是,您缺少Java字符串文字中的反斜杠(’’)字符是轉(zhuǎn)義字符的情況。因此,當(dāng)您想在以JavaString形式編寫(xiě)的正則表達(dá)式中使用’’轉(zhuǎn)義符時(shí),需要對(duì)其進(jìn)行轉(zhuǎn)義。例如
Pattern.compile('.'); // Java Syntax error// A regex that matches a (any) characterPattern.compile('.');// A regex that matches a literal ’.’ characterPattern.compile('.');// A regex that matches a literal ’’ followed by one characterPattern.compile('.');
該String.split(StringseparatorRegex)方法將String拆分為多個(gè)子字符串,這些子字符串由與正則表達(dá)式匹配的子字符串分隔。因此,str.split('.')將拆分str為由單個(gè)文字“。”分隔的子字符串。字符。
解決方法據(jù)我了解,反斜杠點(diǎn)(.)表示任何字符中的一個(gè)字符?因此,由于反斜杠是轉(zhuǎn)義符,因此應(yīng)為反斜杠反斜杠點(diǎn)('.')
這對(duì)字符串有什么作用?我只是在我正在研究的現(xiàn)有代碼中看到了這一點(diǎn)。據(jù)我了解,它將字符串拆分成單個(gè)字符。為什么這樣做,而不是String.toCharArray()。因此,這會(huì)將字符串拆分為一個(gè)字符串?dāng)?shù)組,該數(shù)組的每個(gè)字符串僅包含一個(gè)char?
