Remove duplicate fields using a regular match __string

Source: Internet
Author: User

The syntax of regular expressions is known for its simplicity and efficiency, but one point I is more complex.

There is a recent feature that needs to be removed from the repeated parts of the string, because using the normal traditional kind of comfortable split into an array in one matching way feels a bit inefficient and wasteful, and then thinks of a regular expression to match a duplicate string, The string is replaceall by using string-guided method to remove the corresponding strings.

Target string:

"10110,10254,10554,11025,10254,11002,11532,10110,";

This is an array of numbers, but some of the IDs are duplicated and I have to get rid of them. Since I'm really unfamiliar with regular grammar, it's a temporary indication that the document is in the process of being used. There is no better way of thinking, so go online to look up some ideas.

Find a similar solution to remove duplicate letters with the specific code as follows:

String str = "AAAAAAAHDBKJSDNFKJSNDJKFSDFSDF";   
        str = Str.replaceall ("(? s) (.) (? =.*\\1) "," ");   
        System.out.println (str);
A general look at the meaning of the explanation.

(? s) to open Single-line mode dotall to match any character with the. Number
(.) Any character and captured in the first group
(? =.*\1) This is an assertion that the following content will be any character plus the first set of content captured

First: Open some of the regular mode, it feels like I don't have much relationship, I'm going to remove the duplicate string, he's going to the alphabet.

From the second look at the past, the key should be grouped, the idea is that I have a little impression, or to match a string, and then add an assertion, the third is that assertion, that is, the current string as a grouping, put in the assertion, after the match of the repeated parts. Then I made a change to my needs.

String reg = "(\\d{5},) (? = (\\d{5},) *\\1)";

The specific code is as follows

		String str = "ABCDEABCDEABCDEAAAAAADDDDDCEEEEABCCCCCCCACADAEEC"; 
		String str1 = "10110,10254,10554,11025,10254,11002,11532,10110,";
		String reg = "(\\d{5},) (? = (\\d{5},) *\\1)";
		str = Str.replaceall ("(? s) (.) (? =.*\\1) "," "); 
		STR1 = Str1.replaceall (Reg, "");		str = str.replaceall (Reg, ""); 
		
In the first place it was asserted that there was no plus * causing the match to fail, and I guess it would only match a group if not added.

But then I found that there was a limit to the string array that I would limit to 5. If one of the strings exceeds a 5-digit number, it can cause a match to fail, which may be a problem, because I'm talking about 6 matches for a group and then a set, and that's why there's this issue. If that were the case, it would not have been a problem.

OK, for the time being the problem is solved. This assertion is still a bit difficult to understand, feeling a bit prophetic, but not into the actual matching object inside, the main use is probably the current matching object to make further restrictions and conditions to enhance the correctness of the match. This may require further testing to be known. To do this first, the assertion there are many uses (? <=exp), (? =exp), (?! EXP), (? <!exp), and so on a variety of ways, each will have a variety of subtle differences, this first go down the study, and so have a reliable conclusion on the agreed.

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.