TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
师傅:前面学习了字符组、排除型字符组、字符组简记法、括号、括号的多种用途、量词、以及锚点和环视结构的使用,接下来介绍正则表达式非常有用的功能:匹配模式。
徒弟:哎哟,不错哟!
匹配模式:
作用:改变某些结构的匹配规定
l形式:
I: Case Insensitive
S: SingleLine(dot All)
M:MultiLine
X:Comment
I:不区分大小写的正则表达式匹配。
S:点号可以匹配任意字符,包括换行符
M:更改?^?和?$?的匹配规定,它们可以匹配字符串内部
各行文本的开头和结束位置
X:允许在复杂的正则表达式中添加注释。
I: 不区分大小写
作用:在匹配时,不对英文单词区分大小写
例子:
- import java.util.regex.*;
- public class CaseInsensitive {
- public static void main(String[] args) {
- String str = "abc";
- String regex = "ABC";
- Pattern p = Pattern.compile(regex);
- Matcher m = p.matcher(str);
- if(m.find()){
- System.out.println(str + "能够匹配正则:" + regex);
- }else{
- System.out.println(str + "不能够匹配正则:" + regex);
- }
- }
- }
复制代码 运行结果:
abc不能够匹配正则:ABC
默认情况下区分大小写,所以匹配无法成功。
下面指定不区分大小写模式:
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
运行结果:
abc能够匹配正则:ABC
说明:
int java.util.regex.Pattern.CASE_INSENSITIVE = 2 [0x2]
CASE_INSENSITIVE
public static final int CASE_INSENSITIVE
Enables case-insensitive matching.
By default, case-insensitive matching assumes that only characters inthe US-ASCII charset are being matched. Unicode-aware case-insensitivematching can be enabled by specifying the UNICODE_CASE flag in conjunction with this flag.
Case-insensitive matching can also be enabled via the embedded flag expression (?i).
Specifying this flag may impose a slight performance penalty.
See Also:
Constant Field Values
S: 单行模式
作用:更改点好.的匹配规定,点号也可以匹配换行符
之前在字符组简记法中指出:点号不能匹配换行符
这里指定模式S,则点号也可以匹配换行符。
例子:- import java.util.regex.*;
- public class DotMatchAll {
- public static void main(String[] args) {
- String str = "[url=www.sina.com.cn]
- SINA
- [/url]";
- String regex = "< a href.*";
- Pattern p = Pattern.compile(regex);
- Matcher m = p.matcher(str);
- if(m.find()){
- System.out.println(str + "能够匹配正则:" + regex);
- }else{
- System.out.println(str + "不能够匹配正则:" + regex);
- }
- }
- }
复制代码 运行结果:
<a href=www.sina.com.cn>
SINA
</a>不能够匹配正则:<a href.*</a>
SINA
这里使用了换行符。
那么修改匹配模式:
Pattern p = Pattern.compile(regex,Pattern.DOTALL);
运行结果:
<a href=www.sina.com.cn>
SINA
</a>能够匹配正则:<a href.*</a>
说明:
int java.util.regex.Pattern.DOTALL = 32 [0x20]
DOTALL
public static final int DOTALL
Enables dotall mode.
In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
Dotall mode can also be enabled via the embedded flag expression (?s). (The s is a mnemonic for "single-line" mode, which is what this is called in Perl.)
See Also:
Constant Field Values
源码下载:http://file.javaxxz.com/2014/10/30/000021281.zip |
|