|
步骤 2 : 多个匹配结果 步骤 3 : 大小写敏感 步骤 4 : 大小写不敏感 步骤 5 : 任意字符 步骤 6 : 特殊字符
ABS这样一个字符串也是一个正则表达式,虽然看上去不像
本例在目标字符串里找到一个匹配的结果 import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "ABS";
StringBuffer targetString = new StringBuffer();
targetString.append("1. ABS 041 2. SNIS-556 3. MXBD-197 4. MGD692 5. SNIS-5567 6. ASW-132");
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(targetString);
System.out.println("目标字符串:\t" + targetString);
System.out.println("匹配模式:\t" + p);
boolean found = false;
while (matcher.find()) {
System.out.format("找到匹配的字符串:" + " \"%s\" 开始位置是 " + "index %d 结束位置是 index %d.%n", matcher.group(),
matcher.start(), matcher.end());
found = true;
}
if (!found)
System.out.println("没有找到匹配的字符串");
}
}
SNIS 在目标字符串中出现过多次
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "SNIS";
StringBuffer targetString = new StringBuffer();
targetString.append("1. ABS 041 2. SNIS-556 3. MXBD-197 4. MGD692 5. SNIS-5567 6. ASW-132");
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(targetString);
System.out.println("目标字符串:\t" + targetString);
System.out.println("匹配模式:\t" + p);
boolean found = false;
while (matcher.find()) {
System.out.format(
"找到匹配的字符串:" + " \"%s\" 开始位置是 " + "index %d 结束位置是 index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
if (!found)
System.out.println("没有找到匹配的字符串");
}
}
snis 就找不到匹配的,因为正则表达式是大小写敏感的
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "snis";
StringBuffer targetString = new StringBuffer();
targetString.append("1. ABS 041 2. SNIS-556 3. MXBD-197 4. MGD692 5. SNIS-5567 6. ASW-132");
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(targetString);
System.out.println("目标字符串:\t" + targetString);
System.out.println("匹配模式:\t" + p);
boolean found = false;
while (matcher.find()) {
System.out.format("找到匹配的字符串:" + " \"%s\" 开始位置是 " + "index %d 结束位置是 index %d.%n", matcher.group(),
matcher.start(), matcher.end());
found = true;
}
if (!found)
System.out.println("没有找到匹配的字符串");
}
}
在创建Pattern的时候,使用Pattern.CASE_INSENSITIVE作为第二个参数,即表示大小写不敏感
Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE); import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "snis";
StringBuffer targetString = new StringBuffer();
targetString.append("1. ABS 041 2. SNIS-556 3. MXBD-197 4. MGD692 5. SNIS-5567 6. ASW-132");
Pattern pattern = Pattern.compile(p, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(targetString);
System.out.println("目标字符串:\t" + targetString);
System.out.println("匹配模式:\t" + p);
boolean found = false;
while (matcher.find()) {
System.out.format("找到匹配的字符串:" + " \"%s\" 开始位置是 " + "index %d 结束位置是 index %d.%n", matcher.group(),
matcher.start(), matcher.end());
found = true;
}
if (!found)
System.out.println("没有找到匹配的字符串");
}
}
B. 能同时匹配BS和BD
因为.代表任意字符(不过不能代表换行) import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "B.";
StringBuffer targetString = new StringBuffer();
targetString.append("1. ABS 041 2. SNIS-556 3. MXBD-197 4. MGD692 5. SNIS-5567 6. ASW-132");
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(targetString);
System.out.println("目标字符串:\t" + targetString);
System.out.println("匹配模式:\t" + p);
boolean found = false;
while (matcher.find()) {
System.out.format("找到匹配的字符串:" + " \"%s\" 开始位置是 " + "index %d 结束位置是 index %d.%n", matcher.group(),
matcher.start(), matcher.end());
found = true;
}
if (!found)
System.out.println("没有找到匹配的字符串");
}
}
.是代表任意字符
如果只想匹配"." 这个符号,那么就需要用到元字符\进行转义 \.才真正代表 "." 这个符号 而在Java中,\本身又是转义的意思,所以需要写成 String p = "\\."; import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "\\.";
StringBuffer targetString = new StringBuffer();
targetString.append("1. ABS 041 2. SNIS-556 3. MXBD-197 4. MGD692 5. SNIS-5567 6. ASW-132");
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(targetString);
System.out.println("目标字符串:\t" + targetString);
System.out.println("匹配模式:\t" + p);
boolean found = false;
while (matcher.find()) {
System.out.format("找到匹配的字符串:" + " \"%s\" 开始位置是 " + "index %d 结束位置是 index %d.%n", matcher.group(),
matcher.start(), matcher.end());
found = true;
}
if (!found)
System.out.println("没有找到匹配的字符串");
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2018-03-11
字符串亮了
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|