在Java代码中,拿到一个字符串中国<em>南方航空</em>官网-机票查询,机票预订,我需要把这个字符串中除了<em></em>标签以外的标点符号全部换成中文标点,给出代码和注解
时间: 2024-02-27 18:56:56 浏览: 163
下面是Java代码实现:
```java
public static String replacePunctuation(String content) {
String regex = "(?<=<em>)[^<>/。,!?;:“”‘’《》\\s\\\\]+|[^<>/。,!?;:“”‘’《》\\s\\\\]+(?=</em>)|[^<>/。,!?;:“”‘’《》\\s<>/\\\\]+";
String replacement = "。";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
return matcher.replaceAll(replacement);
}
```
注解:
1. `content`是输入的字符串;
2. `regex`是匹配非中文字符、非数字、非字母、非空格、非常用中文标点、非HTML标签的正则表达式,并且使用了正则表达式的零宽断言;
3. `replacement`是替换成的中文标点,这里使用的是句号;
4. `Pattern.compile(regex)`将正则表达式编译成模式;
5. `matcher.replaceAll(replacement)`将匹配到的字符串替换成`replacement`中的字符串。在这个正则表达式中,我们使用了三个子表达式,分别匹配了<em>标签中的内容、非<em>标签和非中文字符、非数字、非字母、非空格、非常用中文标点、非HTML标签的内容。
阅读全文
相关推荐
















