A look around the regular expression (Java)

Source: Internet
Author: User

All I've written below are reference self-mastery Regular Expressions (third edition).

What is surround

Look around, as the name implies. Regular expressions always work from left to right, but look around also works from right to left. The most typical example is to give you a number such as

12345678, you need to change to 12,345,678 this format. If according to our thinking, it should be from the right to start dividing it, every three numbers plus a comma. Looking around can solve such problems according to our thinking. It is important to note that the look does not match any characters, just matches the position. The effect is similar to ^,&. Surround look is divided into sequential look and reverse look. That is, the difference between working from left to right and right to left. The sequential look in the regular (? =) representation, including parentheses. Reverse order (? <=)

Let's take a simple example to explain the effect of order and reverse look. For example, given a string "hellowld" We want to insert an "or" character before "LD". The following is a simple Java implementation.

         String strtest = "Hellowld";         = Pattern.compile ("(? =ld)");         = Pattern.matcher (strtest);        System.out.printf (Matcher.replaceall ("or"));

The string actually matched in this code is "LD", and the so-called sequential look is in the position before matching the string.

I'm sure you should be able to speculate on the reverse. It matches the position behind the LD.

Example of a surround look

To understand the appeal of the look around us to solve the problem at the beginning. We can analyze it first, we need to meet such conditions. If the number is on the left, the number on the right is a multiple of 3.

The next step is to implement the Java code for this condition.

         String str = "12345678";         = Pattern.compile ("(? <=\\d) (? = (\\d{3}) +$)");         = Pattern.matcher (str);        System.out.printf (Matcher.replaceall (","));

(? <=\\d) This expression satisfies the left is the number, can imagine. Because reversed look is the right position of the matching string, which is the right position of the \d. Does that mean that the left side of this position is \d? The understanding of (? = (\\d{3}) +$) may be a bit less easy. If it is orphaned (\\d{3}) +$ matches the number of left digits from the end of the string to exactly the multiple of 3. But the meaning is different after the order is added. It can match the number of the right number to the position of a multiple of 3. $ is the key, without this symbol, it means that you can not start with the last number to match. This can be combined with a program to think about it well. So the two conditions are fulfilled. Can be matched to the correct position.

However, if the given string is not necessarily a pure number, it may be a form of "12345678$", and the appeal's regular is no use. Here I introduce two more concepts that are negative order look around (?!) and negative reverse (? <!).

In the (? = expression), it means that the expression matches the text to the right, and (?! expression), the expression cannot match the text to the right. Then you can change the regular to

(? <=\\d) (? = (\\d{3}) + (?! \\d)) "Maybe you think you can put (?! \\d) is changed to \d, but these two are actually different, \d is a non-numeric character, which means it must match one character. If the number given to you is "12345678" then you can't get a match.

A look around the regular expression (Java)

Related Article

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.