“正则表达式 案例 结巴去重”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1Eq4y1E79W?p=22”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1Eq4y1E79W?p=22 | https://www.bilibili.com/video/BV1Eq4y1E79W?p=22 | ||
+ | |||
+ | 把类似“我……我要……学学学学……编程java”通过正则表达式修改成“我要学编程java” | ||
+ | |||
+ | === 自己的实现 === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegExp13 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "我我我要要要学学学学编程java!"; | ||
+ | |||
+ | String regStr = "(.)\\1+"; | ||
+ | |||
+ | Pattern pattern = Pattern.compile(regStr); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | while (matcher.find()) { | ||
+ | |||
+ | String str = matcher.group(0); | ||
+ | |||
+ | content = matcher.replaceFirst(str.charAt(0) + ""); | ||
+ | |||
+ | matcher = pattern.matcher(content); | ||
+ | |||
+ | System.out.println("content:" + content); | ||
+ | |||
+ | } | ||
+ | |||
+ | System.out.println("content:" + content); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | content:我要要要学学学学编程java! | ||
+ | content:我要学学学学编程java! | ||
+ | content:我要学编程java! | ||
+ | content:我要学编程java! | ||
+ | </syntaxhighlight> |
2022年11月20日 (日) 08:02的版本
https://www.bilibili.com/video/BV1Eq4y1E79W?p=22
把类似“我……我要……学学学学……编程java”通过正则表达式修改成“我要学编程java”
自己的实现
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp13 {
public static void main(String[] args) {
String content = "我我我要要要学学学学编程java!";
String regStr = "(.)\\1+";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
String str = matcher.group(0);
content = matcher.replaceFirst(str.charAt(0) + "");
matcher = pattern.matcher(content);
System.out.println("content:" + content);
}
System.out.println("content:" + content);
}
}
content:我要要要学学学学编程java!
content:我要学学学学编程java!
content:我要学编程java!
content:我要学编程java!