java正则表达式匹配 [英] java regular expression matching

查看:102
本文介绍了java正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Hi< Dog> Hi < Dog> 在给定的文本中。



更新:



什么正则表达式会匹配这个?

 < FONT FACE =VerdanaSIZE =16COLOR =#0B333CLETTERSPACING =0KERNING = 0>如果您访问网站,请点击以下链接:< FONT SIZE =12>< / FONT>< / FONT> 

< FONT。*?< \ / FONT> code>仅匹配到第一个< / FONT>

解决方案

模式 ^([az] *)< [AZ] *> \ 1 $ 将匹配这些字符串(在rubular.com上看到):

  ab< XYZ> ab 
< XYZ>
bleh< FOO> bleh
<

它不会匹配这些:

 ab< XYZ> de 
x< XYZ> y
FOO< foo> FOO
< / code&

也就是说,该模式类似于


标签 <内容> 标签


同样的 标记 前缀和后缀。标签由零个或多个小写字母组成。内容由零个或多个大写字母组成。前缀部分被匹配,并且被组捕获,然后反向引用<$ c

[...] 用于匹配该字符串, / code>是字符类 。像 [aeiou] 之类的东西匹配任何小写元音之一。 [^ ...] 是一个否定的字符类。 [^ aeiou] 中的任何一个匹配,而是小写元音。

Java字符串文字,模式是^([az] *)< [AZ] *> \\ 1 $

What is the regular expression that can match the following 2 strings.

Hi<Dog>Hi and <Dog> in a given text.

Update:

What regex will match this one?

<FONT FACE="Verdana" SIZE="16" COLOR="#0B333C" LETTERSPACING="0" KERNING="0">If you access the web site click the link below:<FONT SIZE="12"></FONT></FONT>

<FONT.*?<\/FONT> matches only till the first </FONT>

解决方案

The pattern ^([a-z]*)<[A-Z]*>\1$ will match these strings (as seen on rubular.com):

ab<XYZ>ab
<XYZ>
bleh<FOO>bleh
<>

It will not match these:

ab<XYZ>de
x<XYZ>y
FOO<foo>FOO

That is, the pattern is something like

tag<CONTENT>tag

The same tag appears for both the "prefix" and the "suffix". Tag consists of zero or more lowercase letters. Content consists of zero or more uppercase letters. The prefix part is matched and captured by group 1, and then a backreference \1 is used to match that string again for the suffix.

The […] is a character class. Something like [aeiou] matches one of any of the lowercase vowels. [^…] is a negated character class. [^aeiou] matches one of anything but the lowercase vowels.

As a Java string literal, the pattern is "^([a-z]*)<[A-Z]*>\\1$".

这篇关于java正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆