Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the methods of matching, replacing, finding and cutting regular expressions in JAVA

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "JAVA regular expression matching, replacement, search, cutting method introduction", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "JAVA regular expression matching, replacement, search, cutting method introduction" bar!

The search for regular expressions; mainly using split () in the String class

String str

Str.split (); which rule is passed in the method to intercept, and returns an array of String

Common interception rules:

Str.split ("\\.") According to. To intercept

Str.split ("") intercepts by space

Str.split ("cc+") intercepts two or more c characters according to the c character

Str.split ((1)\\. +) intercepts (1) according to where there are two or more characters in the string, indicating that it is grouped into 1.

An example of interception

Intercept in groups; intercept in two or more places.

String str = "publicstaticccvoidddmain"; / / A pair of expressions are grouped and reused String ragex1= "(.)\ 1 +"; String [] ss = str.split (ragex1); for (String st:ss) {System.out.println (st);} / / intercept String ragex = "" by two cc+; / / cut String strs = "publicstaticccvoidddmain"; String ragexs = "cc+"; String [] s = strs.split (ragexs) For (String SSSS: s) {System.out.println (SSSS);} System.out.println ("=-=")

Substitution in regular expression

Syntax definition rules

String s = str.replaceAll (ragex, newstr)

The substitution in the string is replace ()

Replace 4 or more consecutive numbers with *

/ / replace String str= "wei232123jin234"; String ragex = "\ d {4,}"; String newstr = "*"; String s = str.replaceAll (ragex, newstr); System.out.println (s)

Replace the duplicate string with a *

String str = "wwweiei222222jjjiiinnn1232"; String ragex = "(.)\\ 1 +"; String newStr = "*"; String s = str.replaceAll (ragex, newStr); System.out.println (s)

Take me. Me. Want to; want to. Yes. Eat. Eat. I want to eat instead.

String str = "I... Me. Want to; want to. Yes. Eat. Eat. Rice "; String regex ="\\. + "; String newStr ="; str=test (str, regex, newStr); regex =" (.)\\ 1 + "; newStr =" $1 "; test (str, regex, newStr); public static String test (String str, String regex, String newStr) {String str2 = str.replaceAll (regex, newStr); System.out.println (str2); return str2;}

The acquisition of the string of regular expressions

1, create a Pattern object based on the defined regular expression

2, use Pattern object class matching

3. Determine whether it is true or not

4, join the group

Exampl

String str = "public static void main (String [] args)" + "public static void main (String [] args) public static void main (String [] args)"; String ragex = "\\ b [a-zA-Z] {4 zA-Z 5}\\ b"; Pattern p = Pattern.compile (ragex); Matcher m = p.matcher (str); while (m.find ()) {String s = m.group (); System.out.println (s);}

Assignment:

1. Get the user in user

String str = "user"; String regex = "|"; String newStr = ""; String str2 = str.replaceAll (regex, newStr); System.out.println (str2)

2. Get the mailbox number in dhfjksaduirfn 11@qq.com dsjhkfa wang@163.com wokaz

String regex = ""; String [] strs=str.split (regex); for (String str2:strs) {String ragexDemo = "[a-zA-Z0-9] ([a-zA-Z0-9] * [-]? [a-zA-Z0-9] +) *" + "@ ([a-zA-Z0-9] +)\\. [a-zA-Z] +\.? [a-zA-Z] {0th 2}"; Pattern p = Pattern.compile (ragexDemo); Matcher m = p.matcher (str2) While (m.find ()) {System.out.println (m.group ());}}

Sample code:

Import java.util.ArrayList;import java.util.regex.Matcher;import java.util.regex.Pattern;public class test {public static void main (String [] args) {getStrings (); / / use the regular expression to get the specified content in the specified string content System.out.println ("*"); replace () / / replace string contents System.out.println ("*") with regular expressions; strSplit (); / / use regular expressions to cut the string System.out.println ("*"); strMatch () / / string matching} private static void strMatch () {String phone = "13539770000"; / / check whether phone is a qualified mobile phone number (standard: starting with 1, the second place is 3meme 5mem8, and the last 9 digits are arbitrary digits) System.out.println (phone + ":" + phone.matches ("1 [358] [0-9] {9jue 9}"); / / true String str = "abcd12345efghijklmn") / / check whether the str contains 12345 System.out.println (str + ":" + str.matches ("\\ wq12345\\ w+")); / / true System.out.println (str + ":" + str.matches ("\\ wroom123456\\ w+")); / / false} private static void strSplit () {String str = "asfasf.sdfsaf.sdfsdfas.asdfasfdasfd.wrqwrwqer.asfsafasf.safgfdgdsg"; String [] strs = str.split ("\\.") For (Strings: strs) {System.out.println (s);}} private static void getStrings () {String str = "rrwerqq84461376qqasfdasdfrrwerqq84461377qqasfdasdaa654645aafrrwerqq84461378qqasfdaa654646aaasdfrrwerqq84461379qqasfdasdfrrwerqq84461376qqasfdasdf"; Pattern p = Pattern.compile ("qq (. *?) qq"); Matcher m = p.matcher (str); ArrayList strs = new ArrayList (); while (m.find ()) {strs.add (m.group (1)) } for (String s: strs) {System.out.println (s);}} private static void replace () {String str = "asfas5fsaf5s4fs6af.sdaf.asf.wqre.qwr.fdsf.asf.asf.asf"; / / set the. Replace it with _, because. It's a special character, so use\. Expression, and because\ is a special character, it should be used. To express. Str = str.replaceAll ("\\.", "_"); System.out.println (str) }} Thank you for your reading, the above is the "JAVA regular expression matching, replacement, search, cutting method introduction" content, after the study of this article, I believe you on the JAVA regular expression matching, replacement, search, cutting methods to introduce this problem has a more profound experience, the specific use of the need for practice to verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report