“正则表达式 非贪婪匹配”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
(未显示同一用户的4个中间版本) | |||
第48行: | 第48行: | ||
找到:o | 找到:o | ||
找到:o | 找到:o | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 思考:等价于匹配"o"吧 | ||
+ | |||
+ | |||
+ | === 示例:在"oooo"中匹配"o+" === | ||
+ | <syntaxhighlight lang="java"> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegExp08 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "oooo"; | ||
+ | |||
+ | String regStr = "o+"; | ||
+ | |||
+ | Pattern pattern = Pattern.compile(regStr); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | while (matcher.find()) { | ||
+ | |||
+ | System.out.println("找到:" + matcher.group(0)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 找到:oooo | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 示例:在"hello111111 ok"中匹配"\\d+" === | ||
+ | 贪婪匹配<syntaxhighlight lang="java"> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegExp08 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "hello111111 ok"; | ||
+ | |||
+ | String regStr = "\\d+"; | ||
+ | |||
+ | Pattern pattern = Pattern.compile(regStr); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | while (matcher.find()) { | ||
+ | |||
+ | System.out.println("找到:" + matcher.group(0)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 找到:111111 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 示例:在"hello111111 ok"中匹配"\\d+?" === | ||
+ | 非贪婪匹配<syntaxhighlight lang="java"> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegExp08 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String content = "hello111111 ok"; | ||
+ | |||
+ | String regStr = "\\d+?"; | ||
+ | |||
+ | Pattern pattern = Pattern.compile(regStr); | ||
+ | |||
+ | Matcher matcher = pattern.matcher(content); | ||
+ | |||
+ | while (matcher.find()) { | ||
+ | |||
+ | System.out.println("找到:" + matcher.group(0)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight><syntaxhighlight lang="console"> | ||
+ | 找到:1 | ||
+ | 找到:1 | ||
+ | 找到:1 | ||
+ | 找到:1 | ||
+ | 找到:1 | ||
+ | 找到:1 | ||
</syntaxhighlight> | </syntaxhighlight> |
2022年11月18日 (五) 12:44的最新版本
https://www.bilibili.com/video/BV1Eq4y1E79W/?p=15
正则表达式默认是“贪心的”,但灵活运用 ?,可以进行“非贪心”搜索:
?
当此字符紧随任何其他限定符(*、+、?、{n}、{n,}、{n,m})之后时,匹配模式是“非贪心的”。
“非贪心的”模式匹配搜索到的、尽可能短的字符串,而默认的“贪心的”模式匹配搜索得到的是尽可能长的字符串。
例如,在字符串“oooo”中 "o+?"只匹配单个 "o",而 "o+"匹配所有“o”。
示例:在"oooo"中匹配"o+?"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp08 {
public static void main(String[] args) {
String content = "oooo";
String regStr = "o+?";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
找到:o
找到:o
找到:o
找到:o
思考:等价于匹配"o"吧
示例:在"oooo"中匹配"o+"
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp08 {
public static void main(String[] args) {
String content = "oooo";
String regStr = "o+";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
找到:oooo
示例:在"hello111111 ok"中匹配"\\d+"
贪婪匹配
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp08 {
public static void main(String[] args) {
String content = "hello111111 ok";
String regStr = "\\d+";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
找到:111111
示例:在"hello111111 ok"中匹配"\\d+?"
非贪婪匹配
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp08 {
public static void main(String[] args) {
String content = "hello111111 ok";
String regStr = "\\d+?";
Pattern pattern = Pattern.compile(regStr);
Matcher matcher = pattern.matcher(content);
while (matcher.find()) {
System.out.println("找到:" + matcher.group(0));
}
}
}
找到:1
找到:1
找到:1
找到:1
找到:1
找到:1