我想替换字符串中出现的小写字母后跟一个句点后跟一个大写字母(没有任何空格)来包含空格。
例如:
...inconvenient.The...
转换为
...inconvenient. The...
在Java中使用正则表达式的方法是什么?
答案 0 :(得分:3)
我假设您要在小写和 dot 之后插入空格字符,如果后面跟着大写 ..
String s = "foo.Bar and bar.Baz but not FOO.BAR or BAR.baz";
s = s.replaceAll("(?<=[a-z]\\.)(?=[A-Z])", " ");
System.out.println(s); //=> "foo. Bar and bar. Baz but not FOO.BAR or BAR.baz"
说明:
(?<= # look behind to see if there is:
[a-z] # any character of: 'a' to 'z'
\. # '.'
) # end of look-behind
(?= # look ahead to see if there is:
[A-Z] # any character of: 'A' to 'Z'
) # end of look-ahead
答案 1 :(得分:0)
s/([a-z])\.([A-Z])/$1. $2/
答案 2 :(得分:0)
就是这样:
result = subject.replace(/([a-z]\.)([A-Z])/g, "$1 $2");
解释正则表达式
( # group and capture to \1:
[a-z] # any character of: 'a' to 'z'
\. # '.'
) # end of \1
( # group and capture to \2:
[A-Z] # any character of: 'A' to 'Z'
) # end of \2