Home > Java > Java Tutorial > body text

Master the greedy and non-greedy mode skills of Java regular expressions

PHPz
Release: 2023-12-26 14:33:14
Original
812 people have browsed it

Master the greedy and non-greedy mode skills of Java regular expressions

Java regular expression syntax tips: greedy mode and non-greedy mode

When using Java regular expressions, you often encounter the need to match as many characters as possible or as few characters as possible. This requires using greedy mode and non-greedy mode to control the matching method. This article will introduce these two modes in detail and give specific code examples.

1. Greedy mode
Greedy mode is the default matching method. In greedy mode, the regular expression matches as many characters as possible until the matching condition is no longer met.

For example, we have the following text:
String text = "abc abc abc";

We want to match the "abc" in the middle. You can use the following code:
Pattern pattern = Pattern.compile(".*");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {

System.out.println(matcher.group());
Copy after login
Copy after login
Copy after login
Copy after login

}

The output result is "abc abc abc" because ".*" uses greedy mode and will match as many characters as possible.

2. Non-greedy mode
Non-greedy mode is achieved by adding "?" after the greedy mode qualifier. In non-greedy mode, the regular expression matches as few characters as possible until the matching condition is met.

The following is a code example using non-greedy mode:
Pattern pattern = Pattern.compile(".*?");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {

System.out.println(matcher.group());
Copy after login
Copy after login
Copy after login
Copy after login

}

The output result is "abc". This is because ".*?" uses a non-greedy mode and only matches the smallest value that meets the conditions. character.

3. Application scenarios of greedy mode and non-greedy mode
1. Application scenarios of greedy mode
Greedy mode is usually suitable for situations where it is necessary to match as many characters as possible. For example, if we want to match all tags in an HTML document, the default greedy mode can match all tags at once.

String html = "

Heading 1

Paragraph 1

Heading 2

Paragraph 2

";
Pattern pattern = Pattern.compile("<.*?>");
Matcher matcher = pattern.matcher(html);
while (matcher.find()) {

System.out.println(matcher.group());
Copy after login
Copy after login
Copy after login
Copy after login

}

The output results are "

", "

", "

", "

", "", "

", "

".

2. Application scenarios of non-greedy mode
Non-greedy mode is usually suitable for situations where as few characters as possible need to be matched. For example, if we want to match all words in a sentence, the default non-greedy mode can match words one by one.

String sentence = "I love coding";
Pattern pattern = Pattern.compile("\b\w ?\b");
Matcher matcher = pattern.matcher(sentence);
while (matcher.find()) {

System.out.println(matcher.group());
Copy after login
Copy after login
Copy after login
Copy after login

}

The output results are "I", "love", and "coding".

In summary, greedy mode and non-greedy mode are very useful in Java regular expressions. According to specific needs, choosing the appropriate mode can better achieve matching and extract the required information. I hope the code examples in this article can help readers better understand and apply greedy mode and non-greedy mode.

The above is the detailed content of Master the greedy and non-greedy mode skills of Java regular expressions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!