java 表达式写法_java正则表达式的写法

展开全部

方法/步骤

Java正则表达式正则表达式的语法知识:Java支持Perl 5 正则表达式语法的一个子集。e69da5e6ba903231313335323631343130323136353331333337383262一个重要的问题是Java没有正则表达式常量,而是使用简单的老的字符串常量代替的。这就意味着,你需要一个的额外等级的转换。例如,正则表达式\s+不得不表示为“\\s+”这样的字符串。转义序列(Escapesequences):

\

引用后面的元字符(metacharacter) (例如 \. 匹配.) 。

\Q

引用后面所有的元字符直到遇到 \E 。

\E

停止引用元字符 (和 \Q 开始配合使用)。

\\

一个文字反斜杠\。

\uhhhh

Unicode字符 U+hhhh (16进制)。

\xhh

Unicode字符 U+00hh (16进制)。

\cx

ASCII的控制字符 ^x (比如 \cH 可以是 ^H, U+0008)。

\a

ASCII bell 字符 (U+0007)。

\e

ASCII ESC字符r (U+001b)。

\f

ASCII[size=17.280000686645508px]换页字符 (U+000c)。

\n

ASCII 换行字符 (U+000a)。

\r

ASCII 回车字符 (U+000d)。

\t

ASCII tab 字符 (U+0009)。

字符类(Characterclasses)使用集合运算来构建字符类是完全可行的:

[abc]

任意 a, b, 或 c字符。(枚举)

[a-c]

任意a-c范围的字符,即任意 a, b,或 c。 (范围)

[^abc]

除了a, b, 或 c以外的任意字符。 (否定)

[[a-f][0-9]]

任意字符,是a-f,或者0-9。 (联合)

[[a-z]&&[jkl]]

同时满足两个范围的任意字符。 (交叉)

大多数时候,构造的字符类更有用:

\d

任意数字字符。

\D

任意非数字字符。

\s

任意空白字符

\S

任意非空白字符

\w

Any word character.

\W

Any non-word character.

\p{NAME}

Any character in the class with the given NAME.

\P{NAME}

Any character not in the named class.

各种命名的类:

· Unicodecategory names, prefixed by Is. For example \p{IsLu} forall uppercase letters.

· POSIX class names. These are'Alnum', 'Alpha', 'ASCII', 'Blank', 'Cntrl', 'Digit', 'Graph', 'Lower','Print', 'Punct', 'Upper', 'XDigit'.

· Unicode block names, as usedby forName(String) prefixedby In. For example \p{InHebrew} for all characters in the Hebrewblock.

· Character method names. These areall non-deprecated methods from Character whosename starts with is, but with the is replaced by java. Forexample,\p{javaLowerCase}.

定量修饰符(Quantifiers)

定量修饰符匹配一些数量的之前提到的语法表达式。

*

0个或更多

?

0个或1个

+

1个或更多

{n}

确切的n个

{n,}

至少n个

{n,m}

至少n个,不超过m个

定量修饰符默认是贪婪的,意思是它将匹配最长可能的输入序列。也有不贪婪(又称懒惰)的定量修饰符,它匹配最小可能性的输入序列。他们在贪婪方面是一样的,除了后面的?:

*?

Zero or more (non-greedy).

??

Zero or one (non-greedy).

+?

One or more (non-greedy).

{n}?

Exactly n (non-greedy).

{n,}?

At least n (non-greedy).

{n,m}?

At least n but not more than m (non-greedy).

定量修饰符默认允许回溯。也可以避免定量修饰符的回溯。他们除了在后面的+:外,都是一样的。

*+

Zero or more (possessive).

?+

Zero or one (possessive).

++

One or more (possessive).

{n}+

Exactly n (possessive).

{n,}+

At least n (possessive).

{n,m}+

At least n but not more than m (possessive).

零宽断言(Zero-widthassertions)

^

At beginning of line.

$

At end of line.

\A

At beginning of input.

\b

At word boundary.

\B

At non-word boundary.

\G

At end of previous match.

\z

At end of input.

\Z

At end of input, or before newline at end.

四处查看断言(Look-aroundassertions)

四处查看断言主张does(positive)的子模式或doesn't(negative)匹配在(look-ahead)后面或者(look-behind)前面【也就是从当前位置向前或向后查找】不包括包含的匹配文字。向后查找模式可能匹配的最大长度必须不是不受控制的。

(?=a)

Zero-width positive look-ahead.

(?!a)

Zero-width negative look-ahead.

(?<=a)

Zero-width positive look-behind.

(?

Zero-width negative look-behind.

分组(Groups)

(a)

A capturing group.

(?:a)

A non-capturing group.

(?>a)

An independent non-capturing group. (The first match of the subgroup is the only match tried.)

\n

The text already matched by capturing group n.

查看group()的详细信息来了解如何捕捉分组是有限的和可访问的。

运算符(Operators)

ab

Expression a followed by expression b.

a|b

Either expression a or expression b.

标记(Flags)

(?dimsux-dimsux:a)

Evaluates the expression a with the given flags enabled/disabled.

(?dimsux-dimsux)

Evaluates the rest of the pattern with the given flags enabled/disabled.

标记:

i

CASE_INSENSITIVE

case insensitive matching

d

UNIX_LINES

only accept '\n' as a line terminator

m

MULTILINE

allow ^ and $ to match beginning/end of any line

s

DOTALL

allow . to match '\n' ("s" for "single line")

u

UNICODE_CASE

enable Unicode case folding

x

COMMENTS

allow whitespace and comments

任何一个flags的集合可能是空的。例如,(?i-m)可能打开字符敏感,可能关闭多线模式,(?i)可能打开字符敏感,(?-m)可能关闭多线模式。

注意,在Android中,UNICODE字符永远打开:字符不敏感的匹配永远是unicode的。

还有两个另外的标记不可设定的机制:CANON_EQ和LITERAL。

在Android中试图使用CANON_EQ会抛出异常。

实现注释(Implementationnotes)

在Android中的正则表达式实现是由ICU提供的。正则表达式的符号主要是在其他Java语言实现的超集。这就意味着目前的应用程序将如预期的正常工作。但是也有很小的可能性事,Android 可以接受的正则表达式,不能被其他实现所接受。有时候,Android将识别一个简单、特例的、能被更有效处理的正则表达式。这对String中的便利的方法和Pattern中的方法都适用。

END

注意事项

认真学习

温馨提示:亲 答题不易解题更难 您的支持是我继续答题的动力 麻烦采纳 谢谢

本回答被网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值