In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the regular expressions in. *,. What is the meaning of the editor, I think it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. *
. Indicates that any single character except the newline character\ n is matched, and * indicates zero or more times. So. * together means that any character appears zero or more times. No? It means greedy mode. A. roomb, for example, will match the longest string that starts with an and ends with b. If you use it to search for aabab, it matches the entire string aabab. This is called greedy matching.
Another example is the pattern src='. * `, which will match the longest string that begins with src=` and ends with `. Use it to search.
Src= ``test.jpg`width= `60px`height= `80px` will be returned
2. *?
? When used after * or +, it indicates lazy mode. Also known as non-greedy mode. Is to match as few characters as possible. It means matching any number of duplicates, but using the least number of duplicates on the premise that the entire match is successful.
A. matches the shortest string that starts with an and ends with b. If you apply it to aabab, it matches aab (the first to third characters) and ab (the fourth to fifth characters).
Another example is the pattern src='. *? `, which will match the shortest possible string that starts with src=` and ends with `. And there can be no characters between the beginning and the end, because * indicates zero or more. Use it to search.
Src= ``will be returned.
3. +?
Ditto,? When used after * or +, it indicates lazy mode. Also known as non-greedy mode. It means matching any number of duplicates, but using the least number of duplicates on the premise that the entire match is successful.
A. roomroomb matches the shortest string that starts with an and ends with b, but there must be at least one character between an and b. If you apply it to ababccaab, it matches abab (the first to fourth characters) and aab (the seventh to ninth characters). Notice that the matching results are not ab,ab and aab at this time. Because there must be at least one character between an and b.
Another example is the pattern src='. +? `, which will match the string starting with src=` and ending with `as short as possible. And there must be characters between the beginning and the end, because + represents 1 to more. Use it to search.
Src= ``test.jpg` will be returned. Pay attention to. *? Src= ``will not be matched at this time, because there is at least one character between src= `and`.
4. Sample code
Import java.util.regex.Matcher;import java.util.regex.Pattern;import org.junit.jupiter.api.Test;public class TestRegx {@ Test public void testRegx () {String str = "
"; String pattern1 =" src='. * `"; String pattern2 =" src='. *?` "; String pattern3 =" src='. +? `"; Pattern p1 = Pattern.compile (pattern1); Pattern p2 = Pattern.compile (pattern2); Pattern p3 = Pattern.compile (pattern3); Matcher M1 = p1.matcher (str); Matcher m2 = p2.matcher (str); Matcher m3 = p3.matcher (str); System.out.println (" matching results based on pattern1: "); if (m1.find ()) {for (int item0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.