簡體   English   中英

將帶有正則表達式的匹配的一部分插入替換文本中

[英]Inserting a part of a match with a regular expression into a replacement text

我有一些文字:

String s = "The Moon is an astronomical body.it is the fifth-largest natural satellite in the Solar System. the Moon is second-densest satellite among those whose densities are known."

並且我想使用replaseAll方法將每個字符置於點之后或點+空格之后。

s = s.replaceAll("((?<=\\.|\\.\\s)\\p{Lower})","$1".toUpperCase());

一切都很好,除了toUpperCase() 它沒有做任何事情。 為什么? 我怎樣才能讓它發揮作用?

注意:使用相同的模式(可以改善模式)

import java.util.regex.*;

...

StringBuffer str = new StringBuffer( "The Moon is an astronomical body.it is the fifth-largest natural satellite in the Solar System. the Moon is second-densest satellite among those whose densities are known." );
Pattern p = Pattern.compile( "(?<=\\.|\\.\\s)\\p{Lower}" );
Matcher m = p.matcher( str );

while (m.find()) {
    str.setCharAt(m.end()-1, Character.toUpperCase(str.charAt(m.end()-1)) );
}

System.out.print(str);

暫無
暫無

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

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