Java中的量词
先决条件: Java中的正则表达式
Java中的量词允许用户指定匹配的出现次数。以下是Java中一些常用的量词。
X* Zero or more occurrences of X
X? Zero or One occurrences of X
X+ One or More occurrences of X
X{n} Exactly n occurrences of X
X{n, } At-least n occurrences of X
X{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));
}
}
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));
}
}
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));
}
}
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("\nUsing Possessive Quantifier");
Matcher mp = pp.matcher("ggg");
while (mp.find())
System.out.println("Pattern found from " + mp.start() +
" to " + (mp.end()-1));
}
}
输出
Pattern found from 0 to 2
解释:模式g+表示g出现一次或多次。文字是ggg 。即使部分匹配文本也匹配,贪婪匹配器也会匹配最长的文本。在此示例中, g和gg也匹配,但贪婪匹配器生成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 0
Pattern found from 1 to 1
Pattern 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
解释:我们得到与 Greedy 相同的输出,因为整个文本与模式匹配。
贪婪量词和占有量词之间的区别
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("\nUsing Possessive Quantifier");
Matcher mp = pp.matcher("ggg");
while (mp.find())
System.out.println("Pattern found from " + mp.start() +
" to " + (mp.end()-1));
}
}
输出
Using Greedy Quantifier
Pattern found from 0 to 2
Using Possessive Quantifier
在上面的例子中,由于第一个量词是贪婪的,所以g+匹配整个字符串。如果我们将g+与整个字符串匹配,则g+g不匹配,贪婪量词删除最后一个字符,将gg与g+匹配,并找到匹配项。在占有量词中,我们从贪婪开始。 g+匹配整个字符串,但将g+与整个字符串匹配不会将g+g与ggg匹配。与贪婪不同,由于量词是所有格,我们在这一点上停止。