正则表达式
Pattern
. 一个引擎通过解释Pattern执行匹配操作字符序列。A matcher is created from a pattern by invoking the pattern's matcher
method. Once created, a matcher can be used to perform three different kinds of match operations:
Pattern
describes a string pattern.Matcher
tests a string to see if it matches the pattern. PatternSyntaxException
tells you that something wasn't acceptable about the pattern that you tried to define.
二、Regex pattern syntax
. Any character?Zero (0) or one (1) of what came before
* Zero (0) or more of what came before+One (1) or more of what came before
[] A range of characters or digits^Negation of whatever follows (that is, "not whatever")
\d Any digit (alternatively, [0-9])\DAny nondigit (alternatively, [^0-9])
\s Any whitespace character (alternatively, [\n\t\f\r])
\S Any nonwhitespace character (alternatively, [^\n\t\f\r])
\w Any word character (alternatively, [a-zA-Z_0-9])
\W Any nonword character (alternatively, [^\w])
三、test
@Test public void test() {
//Thematches()
method in theMatcher
class matches the regular expression against the whole text
//The methodsstart()
andend()
will give the indexes into the text where the found match starts and ends.
//Find a string of the form A or a followed by zero or more characters, followed by string.
Pattern pattern = Pattern.compile("[Aa].*string"); Matcher matcher = pattern.matcher("A string"); boolean didMatch = matcher.matches(); System.out.println(didMatch); int patternStartIndex = matcher.start(); System.out.println(patternStartIndex); int patternEndIndex = matcher.end(); System.out.println(patternEndIndex); }
---------
true
0
8
@Test public void test1() { //lookingAt() //If your string had more elements than the number of characters in the pattern you searched for, // you could use lookingAt() instead of matches(). // The lookingAt() method searches for substring matches for a specified pattern. Pattern pattern = Pattern.compile("[Aa].*string"); Matcher matcher = pattern.matcher("A string with more than just the pattern."); boolean didMatch = matcher.lookingAt(); System.out.println(didMatch); }
---------
true
@Test public void test2() { //find()
//if multiple matches can be found in the text, thefind()
method will find the first, and then for each subsequent call tofind()
it will move to the next match.
String input = "Here is a WikiWord followed by AnotherWikiWord, then SomeWikiWord."; Pattern pattern = Pattern.compile("[A-Z][a-z]*([A-Z][a-z]*)+"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { Logger.getAnonymousLogger().info("Found this wiki word: " + matcher.group()); } }
--------
Found this wiki word: WikiWord
Found this wiki word: AnotherWikiWord
Found this wiki word: SomeWikiWord
参考文章:
http://tutorials.jenkov.com/java-regex/index.html
https://www.ibm.com/developerworks/library/j-perry-regular-expressions/index.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南