栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Interpretation of regular expression in java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Interpretation of regular expression in java

什么是正则表达式

一个特殊字符串,用于描述匹配一个字符串集合的模式。可以用它来进行字符串的匹配、替换和拆分。

正则表达式语法

用于匹配符合某一特征的字符串

metacharacterDescription
|Find a match for any one of the patterns separated by | as in: cat|dog|fish
.Find just one instance of any character
^Finds a match as the beginning of a string as in: ^Hello
$Finds a match at the end of the string as in: World$
dFind a digit
sFind a whitespace character
bFind a match at the beginning of a word like this: bWORD, or at the end of a word like this: WORDb
uxxxxFind the Unicode character specified by the hexadecimal number xxxx

用于搜索在某一范围内的字符

expressionDescription
[abc]Find one character from the options between the brackets
[^abc]Find one character NOT between the brackets
[0-9]Find one character from the range 0 to 9

用于匹配字符出现的次数

QuantifierDescription
n+Matches any string that contains at least one n
n*Matches any string that contains zero or more occurrences of n
n?Matches any string that contains zero or one occurrences of n
n{x}Matches any string that contains a sequence of X n's
n{x,y}Matches any string that contains a sequence of X to Y n's
n{x,}Matches any string that contains a sequence of at least X n's
匹配字符串

使用String中的matches方法来匹配字符串,返回True或者False

替换和拆分字符串

使用String中的replaceAll方法替换所有匹配的子字符串,类似的方法还有replaceFirst

使用split方法将一个字符串以匹配的分隔符拆分为子字符串

note

正则表达式以元表达式的形式给出,Java中若要使用 要以转义字符 \ 表示

默认情况下,量词符都是贪婪的,如

System.out.println(“Jaaavaa".replaceFirst("a+","R"));

会匹配到 aaa ,可以通过在正则表达式后面加?使量词符变为惰性,如

System.out.println(“Jaaavaa".replaceFirst("a+?","R"));

会匹配到 a 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号