Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b6c5458

Browse files
committed
正则表达式
1 parent d219e7b commit b6c5458

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

data-types/PatternDemo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.regex.Matcher;
2+
import java.util.regex.Pattern;
3+
4+
public class PatternDemo {
5+
public static void main(String[] args) {
6+
String url = "demo.com/users.php?name=zhangsan";
7+
Pattern pattern = Pattern.compile("(\\.(\\w+?))\\?");
8+
Matcher matcher = pattern.matcher(url);
9+
if (matcher.find()) {
10+
// group() 即 group(0),总是返回整个正则表达式命中的结果,这里返回 .php?
11+
System.out.println(matcher.group());
12+
// () 中的即为分组,这里返回 .php
13+
System.out.println(matcher.group(1));
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)