简体   繁体   English

将带有正则表达式的匹配的一部分插入替换文本中

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

I have some 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."

And I want to uppercase every character which is after dot or after dot+space, using replaseAll method. 并且我想使用replaseAll方法将每个字符置于点之后或点+空格之后。

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

Everything is ok, except of toUpperCase() . 一切都很好,除了toUpperCase() It doesnt do anything. 它没有做任何事情。 Why? 为什么? And how can I make it to work? 我怎样才能让它发挥作用?

Note: Using your same pattern (you can improve your pattern) 注意:使用相同的模式(可以改善模式)

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