|
步骤 2 : 匹配数字区间 步骤 3 : 匹配字母区间 步骤 4 : 取反
[BI]S 表示 要么匹配BS,要么匹配IS
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "[BI]S";
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("没有找到匹配的字符串");
}
}
-[0-9] 表示有两个字符,第一个字符是-,第二个字符是0-9的数字
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "-[0-9]";
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("没有找到匹配的字符串");
}
}
[A-Z]- 表示两个字符,第一个字符是A-Z的字母,第二个字符是-
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "[A-Z]-";
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("没有找到匹配的字符串");
}
}
[A-Z][^-][0-9] 表示有三个字符
第一个字符 [A-Z] 字母 第二个字符 [^-] 不能是 - 第三个字符 [0-9] 是数字 import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args) {
String p = "[A-Z][^-][0-9]";
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公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|