Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Four commonly used ways to deal with java regular expressions

Shulou Source: shulou.com Published: 2022-06-02 04:59:23 10月02日 Update

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!

Tags: Character string regular expression processing common way content method three object learning query practical deeper special same important advanced code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Linux Xiaomi Huawei Shulou Tech Info