Java series notes --- Regular Expressions (2), java Regular Expressions

Source: Internet
Author: User

Java series notes --- Regular Expressions (2), java Regular Expressions

Regular Expression

To be honest, regular expressions are really hard to write. When I began to prepare for data collection, I found that more and more information is being collected. The premise of my article is that the article should be clear,

After clarifying my own ideas, I wrote positive expressions in a simple and difficult way. If you think this article is okay, you can pay attention to me first, next, I will update them one after another.

1. What is a regular expression?

Regular expressions is a method used to describe a string set. It is based on the common features of each string in a string set.

Regular Expressions can be used to search, edit, or manipulate text and data. This is a bit of a detour in the official expression. In plain words, regular expressions are mainly used to process text-related content.

There are four common processing methods: 1. Match 2. Cut 3. Replace 4. I will give an example below.

Ii. Common Symbolic Meanings of Regular Expressions

In the regular expression (1), I will give a rough introduction to the common ones. You can refer to them.

3. There are four common processing methods.

(1) MatchingHere I am using the String object method match (String regex ),

1 import java. util. regex. *; 2 public class TestException {3 public static void main (String [] args) throws Exception {4 String tel = "18600000111 "; 5 String reg = "1 [3578] \ d {9}"; // The first letter 1, the second letter 3, 5, 7, 8, followed by a number of 6 boolean b1 = tel. matches (reg); 7 System. out. println (b1); // output result true8} 9}

 

(2) CuttingHere I use the split method in the string.

Case 1: Cut one or more spaces

1 // cut one or more spaces. 2 import java. util. regex. *; 3 public class TestException {4 public static void main (String [] args) throws Exception {5 String str = "aaa bbb ccc ddd eee "; 6 String [] arr = str. split ("+"); // "+" indicates at least one space. 7 for (String s: arr) {8 System. out. print (s); 9} 10} 11}

Running result;

aaabbbcccdddeee

Case 2: use. To cut strings

1 // pass. to cut string 2 import java. util. regex. *; 3 public class TestException {4 public static void main (String [] args) {5 String str2 = "zhangsan. lisi. wangwu "; 6/* \ represents the Escape Character. If you put split (". "), it cannot be cut, because. in the regular expression. it has a unique meaning. 7. When you use an escape character \\. then it only represents a vertex, without special significance */8 String [] arr2 = str2.split ("\\. "); 9 for (String s: arr2) {10 System. out. println (s); 11} 12} 13} 14/* supplement: in java, escape is required. in addition, you need to escape and cannot directly cut: 15 * $ () * + []? \ ^ {} | 16 * after escaping, 17 */

Running result:

zhangsanlisiwangwu

Case 3: Use repeated items for Cutting

1 // use repeated items to cut 2 import java. util. regex. *; 3 public class TestException {4 public static void main (String [] args) {5 String str3 = "wer ####### define UIO *** asdfg "; 6 String reg = "(..) ";//(.) represents the first arbitrary character \ 1 represents going back to the first group of Data + represents one or more 7 String [] arr3 = str3.split (reg); 8 for (String s: arr3) {9 System. out. println (s); 10} 11} 12} 13/* supplement: I am afraid that beginners will "(.) \ 1 + ", I have not understood it yet. I will explain it here :(.) as a whole. \ 1 indicates going back to the first group of data 14 *. It actually represents (.) Here (.), so it is equivalent (.) (.) + here, it is only equivalent to, but also for better understanding. In fact, they still have essential differences 15 * Because (.) = \ 1, that is, if. represents a, then \ 1 represents a, and (.) = (.), we can represent B after a, because the first and second vertices are not the same 16 * I don't know if this is appropriate. It means 17 */

Running result:

wertayuioasdfg

(3) ReplacementString summary Method

Case 1: replace repeated data #

1 // replace repeated data with #2 import java. util. regex. *; 3 public class TestException {4 public static void main (String [] args) {5 String str = "wer #### yw ***** fghj "; 6 // Replace the duplicate data with #7 str = str. replaceAll ("(.) \ 1 + ","#");//(.) the first arbitrary character \ 1 takes the first group of data plus one or more 8 systems. out. println (str); 9} 10}

Running result:

wer#yw#fghj

Case 2: Convert duplicate items into a single

1 import java. util. regex. *; 2 public class TestException {3 public static void main (String [] args) {4 String str = "wer #### yw *** fg ??? Hj "; 5 // the meaning of the last parameter can be referenced in the group of the first parameter using a $ number. This dollar symbol indicates that the content in the parentheses is 6 str = str. replaceAll ("(.) \ 1 + "," $1 "); 7 System. out. println (str); 8} 9}

Running result:

wer#yw*fg?hj

Case 3: number of digits in the middle of the phone number expressed *

1 import java. util. regex. *; 2 public class TestException {3 public static void main (String [] args) {4 String str2 = "15889895644 "; // 158 *** 5644 5 str2 = str2.replaceAll ("(\ d {3}) \ d {4} (\ d {4 })", "$1 ***** $2"); 6 System. out. println (str2); // $1 indicates the first parentheses before the symbol, and $2 indicates the second. If the third parentheses exist, you can also $3; 7} 8}

Running result:

158****5644

(4) ObtainThe string does not directly provide this function. It can only be matched through regular expressions.

Case 1: Obtain the string matching the Regular Expression

1 import java. util. regex. *; 2 public class TestException {3 public static void main (String [] args) {4 String str = "da jio zhu yi laa, ming tian fang jia laa "; 5 // 1. define Rule 6 String reg = "\ B [a-z] {3} \ B "; // any three characters \ B are the boundary of the word (see why this is added) 7 Pattern p = Pattern. compile (reg); 8 // 3. get the matching object through the regular expression object and associate the operation string with 9 Matcher m = p. matcher (str); 10 while (m. find () {// find () is used to search for any target string that matches the regular expression 11 System. out. println (m. s Tart () + "..... "+ m. group () + "... "+ m. end (); 12} // start () start position group () is used to return the end position 13} 14} 15/* of the string containing the matched text: in the regex (Regular Expression) package, there are two classes: Pattern and Matcher ). 16 * You can also learn more about this. 17 */

Running result:

3.....jio...67.....zhu...1014.....laa...1735.....jia...3839.....laa...42

4. A comprehensive small case study

Question 1:10.10.10.10 192.168.118.40 192.168.1.200 127.0.0.108 in ascending order

1 import java. util. arrays; 2 import java. util. regex. *; 3 public class TestException {4 public static void main (String [] args) {5 String ip = "10.10.10.10 192.168.118.40 192.168.1.200 127.0.0.108 "; 6/* 7 * to make it easier for each end to complete zero, ensure that each field is at least three 8 */9 ip = ip. replaceAll ("(\ d +)", "00 $1"); // fill in 0, so that at least three digits 10 11 ip = ip. replaceAll ("0 * (\ d {3})", "$1"); // all data is changed to three digits 12 13 String [] ips = ip. split ("+"); // use spaces to cut 14 Arrays. sort (ips); // sorted in ascending order by 15 for (String x: ips) {16 System. out. println (x. replaceAll ("0 * (\ d +)", "$1"); // restore 17} 18} 19} 20/** this question may not be difficult, it is difficult to think in the way of thinking. Every step of this process is critical. I also hope that you will think more on your way to learning, rather than staying on the basis of watching */

Running result:

10.10.10.10127.0.0.108192.168.1.200192.168.118.40

This article ends here. I will write more regular expressions, such as Pattern and Matcher ), for example, how to obtain the phone number in the text and so on, but I should not write it recently,

Next, I will write some other related knowledge.

You are also welcome to make comments after reading this article. If you are not thoughtful about writing or can make it better, you are also welcome to submit it. I will correct it immediately. Thank you!

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.