“正则表达式 案例 结巴去重”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (→自己的实现) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的2个中间版本) | |||
第78行: | 第78行: | ||
=== 视频中的原实现 === | === 视频中的原实现 === | ||
+ | <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!"; | ||
+ | |||
+ | //1、去掉所有的. | ||
+ | Pattern pattern = Pattern.compile("\\."); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | content = matcher.replaceAll(""); | ||
+ | |||
+ | System.out.println("content=" + content); | ||
+ | |||
+ | //2.去掉重复的字 | ||
+ | // 思路 | ||
+ | //(1)使用 (.)\\1+ | ||
+ | //(2)使用 反向引用$1 来替换匹配到的内容 | ||
+ | // 注意:因为正则表达式变化,所以需要重置 matcher | ||
+ | pattern = Pattern.compile("(.)\\1+");//分组的捕获内容记录到$1 | ||
+ | |||
+ | matcher = pattern.matcher(content); | ||
+ | |||
+ | while (matcher.find()) { | ||
+ | |||
+ | System.out.println("找到=" + matcher.group(0)); | ||
+ | |||
+ | } | ||
+ | |||
+ | //使用 反向引用$1 来替换匹配到的内容 | ||
+ | String newContent = matcher.replaceAll("$1"); | ||
+ | |||
+ | System.out.println("newContent:" + newContent); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | content=我我要学学学学编程java! | ||
+ | 找到=我我 | ||
+ | 找到=学学学学 | ||
+ | newContent:我要学编程java! | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | === 视频中的一行代码实现 === | ||
+ | <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!"; | ||
+ | |||
+ | //1、去掉所有的. | ||
+ | Pattern pattern = Pattern.compile("\\."); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | content = matcher.replaceAll(""); | ||
+ | |||
+ | System.out.println("content=" + content); | ||
+ | |||
+ | //2.去掉重复的字 | ||
+ | // 思路 | ||
+ | //(1)使用 (.)\\1+ | ||
+ | //(2)使用 反向引用$1 来替换匹配到的内容 | ||
+ | // 注意:因为正则表达式变化,所以需要重置 matcher | ||
+ | String newContent = Pattern.compile("(.)\\1+").matcher(content).replaceAll("$1"); | ||
+ | |||
+ | System.out.println("newContent:" + newContent); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | content=我我要学学学学编程java! | ||
+ | newContent:我要学编程java! | ||
+ | </syntaxhighlight> |
2022年11月20日 (日) 10:10的最新版本
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!
示例:更好的外部反向引用实现方式
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);
//使用 反向引用$1 来替换匹配到的内容
String newContent = matcher.replaceAll("$1");
System.out.println("newContent:" + newContent);
}
}
newContent:我要学编程java!
视频中的原实现
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp13 {
public static void main(String[] args) {
String content = "我....我要....学学学学....编程java!";
//1、去掉所有的.
Pattern pattern = Pattern.compile("\\.");
Matcher matcher = pattern.matcher(content);
content = matcher.replaceAll("");
System.out.println("content=" + content);
//2.去掉重复的字
// 思路
//(1)使用 (.)\\1+
//(2)使用 反向引用$1 来替换匹配到的内容
// 注意:因为正则表达式变化,所以需要重置 matcher
pattern = Pattern.compile("(.)\\1+");//分组的捕获内容记录到$1
matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到=" + matcher.group(0));
}
//使用 反向引用$1 来替换匹配到的内容
String newContent = matcher.replaceAll("$1");
System.out.println("newContent:" + newContent);
}
}
content=我我要学学学学编程java!
找到=我我
找到=学学学学
newContent:我要学编程java!
视频中的一行代码实现
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp13 {
public static void main(String[] args) {
String content = "我....我要....学学学学....编程java!";
//1、去掉所有的.
Pattern pattern = Pattern.compile("\\.");
Matcher matcher = pattern.matcher(content);
content = matcher.replaceAll("");
System.out.println("content=" + content);
//2.去掉重复的字
// 思路
//(1)使用 (.)\\1+
//(2)使用 反向引用$1 来替换匹配到的内容
// 注意:因为正则表达式变化,所以需要重置 matcher
String newContent = Pattern.compile("(.)\\1+").matcher(content).replaceAll("$1");
System.out.println("newContent:" + newContent);
}
}
content=我我要学学学学编程java!
newContent:我要学编程java!