In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "four common ways to deal with java regular expressions". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "four common ways to deal with java regular expressions".
Java regular expression advanced article, introduces four commonly used processing methods: matching, segmentation, substitution, acquisition, the specific content is as follows
Package test; import java.util.regex.Matcher; import java.util.regex.Pattern; / * regular expressions * regular expressions are mainly used in four ways: match, split, replace, and get. * use some simple symbols to represent the operation of the code * @ author cyc * * / public class Rex {public static void main (String [] args) {/ / handle Rex reg = new Rex () for strings; / / verify the reg regular expression of qq / / where\ w refers to [a-zA-Z0-9], and it is important that?, *. + these three respectively / /? It means one or no occurrence, / / + means one or n times, / / * means 0 or n times, / / there are some special words X {n} exactly n times X {n,} at least n times, X {nforce m} n times to m times, String mathReg = "[1-9]\ d {4 # 19}"; String divisionReg = "(.)\\ 1 +" / /\\ b refers to the boundary value String getStringReg = "\\ b\\ w {3}\\ b"; / / string matching (the first is the string divided by 0) reg.getMatch ("739295732", mathReg); reg.getMatch ("039295732", mathReg); / / replacement of strings / / removal of reduplicated words reg.getReplace ("12111123ASDASDAAADDD", divisionReg, "$1") / / Segmentation of strings / / Segmentation of reduplicated words, repeated / / We need to know the concept of a group here (.)\ 1 the second and first to the same reg.getDivision ("aadddddasdasdasaaaaaassssfq", divisionReg); / / string acquisition / / now get three strings to take out reg.getString ("ming tian jiu yao fangjia le", getStringReg) } / * get the string of the query * extract the matching string * / private void getString (String str, String regx) {/ / 1. Encapsulate the expression in progress into an object Patten class to implement Pattern pattern = Pattern.compile (regx); / / 2. Associate a string with a regular expression Matcher matcher = pattern.matcher (str); the matches method in the / / 3.String object is implemented through this Matcher and pattern. System.out.println (matcher.matches ()); / / find the substring while (matcher.find ()) {/ / get the string System.out.println (matcher.group ()); / / get the first and last positions of the string System.out.println (matcher.start () + "- -" + matcher.end ()) }} / * string segmentation * / private void getDivision (String str, String regx) {String [] dataStr = str.split (regx); for (String s:dataStr) {System.out.println ("regular expression segmentation +" + s) }} / * string substitution * / private void getReplace (String str, String regx,String replaceStr) {String stri = str.replaceAll (regx,replaceStr); System.out.println ("regular expression substitution" + stri) } / * string processing matching * match method * / public void getMatch (String str, String regx) {System.out.println ("regular expression matching" + str.matches (regx)) in the String class;}} so far, I believe you have a deeper understanding of the "four common ways to handle java regular expressions". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.