how2j.cn

步骤 1 : 匹配多个字符中的某一个   
步骤 2 : 匹配数字区间   
步骤 3 : 匹配字母区间   
步骤 4 : 取反   

步骤 1 :

匹配多个字符中的某一个

edit
[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("没有找到匹配的字符串"); } }
步骤 2 :

匹配数字区间

edit
-[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("没有找到匹配的字符串"); } }
步骤 3 :

匹配字母区间

edit
[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公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 Temp-正则表达式-一组字符 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图