Java中的量词

先决条件: Java中的正则表达式

null

Java中的量词允许用户指定匹配的出现次数。下面是Java中一些常用的量词。

X*        Zero or more occurrences of XX?        Zero or One occurrences of XX+        One or More occurrences of XX{n}      Exactly n occurrences of X X{n, }    At-least n occurrences of XX{n, m}   Count of occurrences of X is from n to m

上述量词可以是贪婪的、不情愿的和占有的。

贪婪量词(默认)

默认情况下,量词是贪婪的。贪婪的量词试图匹配与给定模式匹配的最长文本。贪婪量词的工作原理是在尝试任何匹配之前先读取整个字符串。如果整个文本不匹配,请删除最后一个字符并重试,重复此过程直到找到匹配项。

JAVA

// Java program to demonstrate Greedy Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test
{
public static void main(String[] args)
{
// Making an instance of Pattern class
// By default quantifier "+" is Greedy
Pattern p = Pattern.compile( "g+" );
// Making an instance of Matcher class
Matcher m = p.matcher( "ggg" );
while (m.find())
System.out.println( "Pattern found from " + m.start() +
" to " + (m.end()- 1 ));
}
}


输出

Pattern found from 0 to 2

说明: 模式 g+ 指一次或多次 G .文本是 ggg .贪婪匹配器将匹配最长的文本,即使匹配文本的部分也匹配。在这个例子中, G 游戏打得好 同样匹配,但贪婪的匹配者产生 ggg .

不情愿的量词(在量词后加一个?)

这个量词使用的方法与贪婪量词相反。它从第一个字符开始,一次处理一个字符。

JAVA

// Java program to demonstrate Reluctant Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test
{
public static void main(String[] args)
{
// Making an instance of Pattern class
// Here "+" is a Reluctant quantifier because
// a "?' is appended after it.
Pattern p = Pattern.compile( "g+?" );
// Making an instance of Matcher class
Matcher m = p.matcher( "ggg" );
while (m.find())
System.out.println( "Pattern found from " + m.start() +
" to " + (m.end()- 1 ));
}
}


输出

Pattern found from 0 to 0Pattern found from 1 to 1Pattern found from 2 to 2

说明: 由于量词是不情愿的,它将测试的最短部分与模式匹配。它一次处理一个字符。

所有格量词(在量词后加+)

这个量词匹配尽可能多的字符,就像贪婪的量词一样。但如果整个字符串不匹配,则不会尝试从结尾删除字符。

JAVA

// Java program to demonstrate Possessive Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test
{
public static void main(String[] args)
{
// Making an instance of Pattern class
// Here "+" is a Possessive quantifier because
// a "+' is appended after it.
Pattern p = Pattern.compile( "g++" );
// Making an instance of Matcher class
Matcher m = p.matcher( "ggg" );
while (m.find())
System.out.println( "Pattern found from " + m.start() +
" to " + (m.end()- 1 ));
}
}


输出

Pattern found from 0 to 2

说明: 我们得到与贪婪相同的输出,因为整个文本与模式匹配。

贪婪量词和所有格量词的区别

JAVA

// Java program to demonstrate difference
// between Possessive and Greedy Quantifiers
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test
{
public static void main(String[] args)
{
// Create a pattern with Greedy quantifier
Pattern pg = Pattern.compile( "g+g" );
// Create same pattern with possessive quantifier
Pattern pp = Pattern.compile( "g++g" );
System.out.println( "Using Greedy Quantifier" );
Matcher mg = pg.matcher( "ggg" );
while (mg.find())
System.out.println( "Pattern found from " + mg.start() +
" to " + (mg.end()- 1 ));
System.out.println( "Using Possessive Quantifier" );
Matcher mp = pp.matcher( "ggg" );
while (mp.find())
System.out.println( "Pattern found from " + mp.start() +
" to " + (mp.end()- 1 ));
}
}


输出

Using Greedy QuantifierPattern found from 0 to 2Using Possessive Quantifier

在上面的例子中,由于第一个量词是贪婪的, g+ 匹配整个字符串。如果我们匹配 g+ 用整根绳子, g+g 不匹配,贪婪量词删除最后一个字符,匹配 游戏打得好 具有 g+ ,并找到匹配项。在所有格量词中,我们从贪婪开始。 g+ 匹配整个字符串,但匹配 g+ 整根绳子都不匹配 g+g 具有 ggg .与贪婪不同,因为量词是所有格的,所以我们到此为止。

本文由 阿加瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享