[Android基础]Pattern与Matcher

本文深入探讨Java中正则表达式的应用,包括模式匹配、字符串分隔、全字符串匹配及部分匹配方法。通过实例演示如何使用正则表达式进行高效文本处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://blog.youkuaiyun.com/cclovett/article/details/12448843


import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test 
{	
	public static void main(String[] args) 
	{	
		//pattern()返回正则表达式的字符串形式
		//输出:\w+
		Pattern p1 = Pattern.compile("\\w+");	
		System.out.println(p1.pattern());
		
		//split(CharSequence input)分隔字符串
		//输出:s[0]=qq是   s[1]=电话是
		Pattern p2 = Pattern.compile("\\d+");
		String[] s = p2.split("qq是123电话是456");
		for (int i = 0; i < s.length; i++) {
			System.out.println(s[i]);
		}
		
		//matcher(String regex,CharSequence input)只匹配一次,且匹配全部字符串
		System.out.println(Pattern.matches("\\d+", "123"));//true
		System.out.println(Pattern.matches("\\d+", "123啊"));//false
		
		//matcher.pattern()返回该Matcher对象是由哪个Pattern对象的创建的
		//输出:\d+
		Pattern p3 = Pattern.compile("\\d+");
		Matcher m3 = p3.matcher("123");
		System.out.println(m3.pattern().pattern());
		
		
		/////////////////Matcher的三种匹配方式
		//Matcher.matches()/ Matcher.lookingAt()/ Matcher.find()
		
		
		//matches()对整个字符串进行匹配,只有整个字符串都匹配了才返回true
		Pattern p4 = Pattern.compile("\\d+");
		Matcher m4 = p4.matcher("123abc");
		System.out.println(m4.matches());//false
		Matcher m42 = p4.matcher("123");
		System.out.println(m42.matches());//true
		
		//lookingAt()对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
		Pattern p5 = Pattern.compile("\\d+");
		Matcher m5 = p5.matcher("123abc");
		System.out.println(m5.lookingAt());//true
		Matcher m52 = p5.matcher("abc123");
		System.out.println(m52.lookingAt());//false
		
		//find()对字符串进行匹配,匹配到的字符串可以在任何位置
		Pattern p6 = Pattern.compile("\\d+"); 
		Matcher m6 = p6.matcher("11aa"); 
		System.out.println(m6.find());//true
		Matcher m62 = p6.matcher("aa11"); 
		System.out.println(m62.find());//true
		Matcher m63 = p6.matcher("aa"); 
		System.out.println(m63.find());//false
		
		
		//当使用matches(),lookingAt(),find()执行匹配操作后,就可以利用以上三个方法得到更详细的信息. 
		//start()返回匹配到的子字符串在字符串中的索引位置. 
		//end()返回匹配到的子字符串的最后一个字符在字符串中的索引位置. 
		//group()返回匹配到的子字符串
		
		
		Pattern p7 = Pattern.compile("\\d+"); 
		Matcher m7 = p7.matcher("aaa123bbb"); 
		System.out.println(m7.find());//true
		System.out.println(m7.start());//3
		System.out.println(m7.end());//6 (是123的后一位)
		System.out.println(m7.group());//123
		
		
		//start(),end(),group()均有一个重载方法它们是start(int i),end(int i),group(int i)专用于分组操作
		//Mathcer类还有一个groupCount()用于返回有多少组
		
		
		Pattern p8 = Pattern.compile("([a-z]+)(\\d+)"); 
		Matcher m8 = p8.matcher("aaa123bbb"); 
		System.out.println(m8.find());//true
		System.out.println(m8.groupCount());//2
		System.out.println(m8.group(1));//aaa
		System.out.println(m8.group(2));//123		
		System.out.println(m8.start(1));//0
		System.out.println(m8.end(1));//3 
		System.out.println(m8.start(2));//3
		System.out.println(m8.end(2));//6 
		
		Pattern p9 = Pattern.compile("\\d+"); 
		Matcher m9 = p9.matcher("我的QQ是:12 我的电话是:34 我的邮箱是:aaa56@aaa.com"); 
		while(m9.find()) {
			//每次执行匹配操作后start(),end(),group()三个方法的值都会改变,改变成匹配到的子字符串的信息,
			//以及它们的重载方法,也会改变成相应的信息
			//只有当匹配操作成功,才可以使用start(),end(),group()三个方法,否则会抛出java.lang.IllegalStateException,
			//也就是当matches(),lookingAt(),find()其中任意一个方法返回true时,才可以使用
			//12 34 56
		    System.out.println(m9.group());
		    System.out.println(m9.start());
		    System.out.println(m9.end());
		}
	}
}

打印台输出: 🔍 正在获取应用 com.baidu.searchbox 的所有权限... 🔧 [DEBUG] 执行命令: adb shell pm dump com.baidu.searchbox 🔧 [DEBUG] ADB 原始输出长度: 456188 字符 🔧 [DEBUG] 输出截取(前500字符): DUMP OF SERVICE package: Activity Resolver Table: Full MIME Types: application/x-zip-compressed: 7f4861d com.baidu.searchbox/.download.center.ui.DownloadedCategorySecActivity filter b682092 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Category: "android.intent.category.BROWSABLE" Scheme: "content" Scheme: "file" Path: "PatternMatcher{GLOB: .*\.zip}" Path: "PatternMatcher{GLOB: .*\.\w+\.zip}" Path: "PatternMatcher{GLOB: .*\.7z}" Path: "PatternMatcher{GLOB: .*\.\w+\.7z}" Path: "PatternMatcher{GLOB: .*\.rar}" Path: "PatternMatcher{GLOB: .*\.\w+\.rar}" StaticType: "application/zip" StaticType: "application/x-zip-compressed" StaticType: "application/x-7z-compressed" StaticType: "application/rar" StaticType: "application/x-rar-compressed" application/pdf: c611dca com.baidu.searchbox/.plugins.PDFViewerActivity filter a96b3b Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" StaticType: "application/pdf" application/rar: 7f4861d com.baidu.searchbox/.download.center.ui.DownloadedCategorySecActivity filter b682092 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Category: "android.intent.category.BROWSABLE" Scheme: "content" Scheme: "file" Path: "PatternMatcher{GLOB: .*\.zip}" Path: "PatternMatcher{GLOB: .*\.\w+\.zip}" Path: "PatternMatcher{GLOB: .*\.7z}" Path: "PatternMatcher{GLOB: .*\.\w+\.7z}" Path: "PatternMatcher{GLOB: .*\.rar}" Path: "PatternMatcher{GLOB: .*\.\w+\.rar}" StaticType: "application/zip" StaticType: "application/x-zip-compressed" StaticType: "application/x-7z-compressed" StaticType: "application/rar" StaticType: "application/x-rar-compressed" application/zip: 7f4861d com.baidu.searchbox/.download.center.ui.DownloadedCategorySecActivity filter b682092 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Category: "android.intent.category.BROWSABLE" Scheme: "content" Scheme: "file" Path: "PatternMatcher{GLOB: .*\.zip}" Path: "PatternMatcher{GLOB: .*\.\w+\.zip}" Path: "PatternMatcher{GLOB: .*\.7z}" Path: "PatternMatcher{GLOB: .*\.\w+\.7z}" Path: "PatternMatcher{GLOB: .*\.rar}" Path: "PatternMatcher{GLOB: .*\.\w+\.rar}" StaticType: "application/zip" StaticType: "application/x-zip-compressed" StaticType: "application/x-7z-compressed" StaticType: "application/rar" StaticType: "application/x-rar-compressed" application/vnd.openxmlformats-officedocument.presentationml.presentation: 170d258 com.baidu.searchbox/.plugins.PPTViewerActivity filter 55b4c96 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.pptx}" StaticType: "application/vnd.openxmlformats-officedocument.presentationml.presentation" application/vnd.ms-powerpoint: 170d258 com.baidu.searchbox/.plugins.PPTViewerActivity filter 5308fb1 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.ppt}" StaticType: "application/vnd.ms-powerpoint" application/vnd.openxmlformats-officedocument.wordprocessingml.document: b398017 com.baidu.searchbox/.plugins.DOCViewerActivity filter 20d75ed Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.docx}" StaticType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" audio/*: b35adee com.baidu.searchbox/.systemshare.SystemShareToNetDisk filter 4edc28f Action: "android.intent.action.SEND" Category: "android.intent.category.DEFAULT" StaticType: "image" StaticType: "video" StaticType: "audio" mPriority=0, mOrder=0, mHasStaticPartialTypes=true, mHasDynamicPartialTypes=false application/vnd.ms-excel: bf79c22 com.baidu.searchbox/.plugins.EXCELViewerActivity filter e4c06b3 Action: "android.intent.action.VIEW" Category: "android.intent.category.DEFAULT" Scheme: "file" Scheme: "content" Path: "PatternMatcher{GLOB: .*\.xls}" StaticType: "application/vnd.ms-excel" application/x-rar-c... 🔧 [DEBUG] 找到 0 个权限声明段落 🔧 [DEBUG] 总共找到 0 个有效权限 ⚠️ dumpsys 未找到权限,尝试解析 APK 中的 AndroidManifest.xml... ❌ 未找到任何权限声明,请检查 ADB 输出或手动提供权限列表
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值