📄 e433. setting case sensitivity in a regular expression.txt
字号:
By default, a pattern is case-sensitive. By adding a flag, a pattern can be made case-insensitive.
It is also possible to control case sensitivity within a pattern using the inline modifier (?i). The inline modifier affects all characters to the right and in the same enclosing group, if any. For example, in the pattern a(b(?i)c)d, only c is allowed to be case-insensitive. You can also force case sensitivity with (?-i).
The inline modifier can also contain pattern characters using the form (?i:abc). In this case, only those pattern characters inside the inline modifier's enclosing group are affected. This form does not capture text (see e436 Capturing Text in a Group in a Regular Expression).
CharSequence inputStr = "Abc";
String patternStr = "abc";
// Compile with case-insensitivity
Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches(); // true
// Use an inline modifier
matchFound = pattern.matches("abc", "aBc"); // false
matchFound = pattern.matches("(?i)abc", "aBc"); // true
matchFound = pattern.matches("a(?i)bc", "aBc"); // true
// Use enclosing form
matchFound = pattern.matches("((?i)a)bc", "aBc"); // false
matchFound = pattern.matches("(?i:a)bc", "aBc"); // false
matchFound = pattern.matches("a((?i)b)c", "aBc"); // true
matchFound = pattern.matches("a(?i:b)c", "aBc"); // true
// Use a character set
matchFound = pattern.matches("[a-c]+", "aBc"); // false
matchFound = pattern.matches("(?i)[a-c]+", "aBc"); // true
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -