In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Java canonical correlation Pattern and Matcher class example analysis, the article introduces in great detail, has a certain reference value, interested friends must read it!
Let's first talk about the three methods in Matcher (the result taken takes the group () method as an example)
Matches (): the whole match returns True only if the entire character sequence is completely matched, otherwise it returns False. However, if the first part matches successfully, the position of the next match will be moved. For example, if the string is "A123" and the regular expression is "\ w\ d\ d\ d", then the matches () method returns true. In other words, the string to be matched needs to correspond to the regular expression one by one. Letters correspond to letters and numbers correspond to numbers, so it is called a perfect match. (it took me a long time to understand here, and what I found on the Internet didn't mention it. Maybe I don't have enough comprehension.)
Find (): partial match, starting from the current position, finding a matching substring, will move the next matching position. "find () traverses the input string forward like an iterator." -- from "Java programming ideas." Looking at the find () method in the following example, the string to be matched is "a123b", the regular expression is "\ d\ d\ d", and the final output is: 123, so it is called a partial match, and true is returned as long as there is one.
LookingAt (): partial match. Always match from the first character. If the match succeeds, it will not continue to match. If the match fails, it will not continue. The lookingAt () method is between the matches () and find () methods. Partial matching of the lookingAt () method means matching starts from the first bit. If the first bit does not match, it no longer matches, and returns false directly, if the first bit matches, the second bit is matched, and so on. Example: the string to match is "a123b", the regular expression is "\ w\ d\ d", and the output is "A12". It is easy to understand that it is a bit-by-bit match, and the next bit is matched when it is matched. "\ w\ d\ d" this regular stands for "alphanumeric", so the output happens to be A12.
All right, test the code:
Package com.wjj.utils;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** @ author author: mustard * @ version creation time: August 18, 2016. class description: regular expression exercise * / public class Regex {/ / find method test public static void find (String html) {String regex = "\\ d\\ d\\ d" Pattern pattern = Pattern.compile (regex, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher (html); System.out.print ("find ():"); if (matcher.find ()) {System.out.println (matcher.group ());}} / / matches method test public static void matches (String html) {String regex = "^\ w\\ d\\ d\\ d" Pattern pattern = Pattern.compile (regex, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher (html); System.out.print ("matches ():"); if (matcher.matches ()) {System.out.println (matcher.group ());}} / / lookingAt method Test public static void lookingAt (String html) {String regex = "\\ w\\ d\\ d" Pattern pattern = Pattern.compile (regex, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher (html); System.out.print ("lookingAt ():"); if (matcher.lookingAt ()) {System.out.println (matcher.group ());} public static void main (String [] args) {/ / find method Test find ("a123b"); / / matches method Test matches ("a123") / / lookingAt method to test lookingAt ("a123b");}}
Output result:
Find (): 123
Matches (): A123
LookingAt (): A12
Summary: regular expressions are not difficult in themselves, just write them according to the rules when you use them. It took me a long time to understand the three methods of matches, find, and lookingAt in my study, but I couldn't understand what perfect matching and partial matching meant. Finally, I found the answer I wanted on stackoverflow.
The above is all the content of the article "sample Analysis of Java regular related Pattern and Matcher classes". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.