Regular Expressions remove duplicate words

Source: Internet
Author: User

Cause: it is still a problem when a chart is dynamically generated. Because fields are dynamically set, you can set fields in Multiple Axes and sequences, which may lead to repeated fields. Duplicate fields generate SQL statements and execute them. However, the execution results generate a List (execute the sqlquery of Hibernate. setResultTransformer (Transformers. ALIAS_TO_ENTITY_MAP ). list () is problematic (an exception is thrown ). Therefore, we need to remove the repeated fields in the query SQL field. The first thing that comes to mind is to separate strings into arrays, add them to the set, and then generate a non-repeated field name string with the field names in the Set. The Code is as follows: [java] String s = "D_NAME, COMPANYID, COMPANY_NAME, PARENT_ID, ID, COMPANYID, COMPANY_NAME, DF_UNIFIED_CODE, DF_CLEARANCE_TAG_ID,"; String [] s1 = s. split (","); Set set = new HashSet (); for (String ss: s1) {set. add (ss);} Iterator iter = set. iterator (); String newStr = ""; while (iter. hasNext () {newStr + = iter. the result of next ();} shows that repeated fields are not removed. Change set. add (ss); To [java] if (! Set. contains (ss) set. add (ss); the result is still. I don't know how java processes strings. I would like to ask a java veteran to say that the Set should not be repeated in theory, but there is no effective solution for such a result. Which prawn can tell me what is going on? Then I want to use a regular expression to remove the repeated field names of strings. The first expression: (\ B [\ w | _] + \ B ,?) (. *) \ 1 (. *), CODE: [java] String s = "LOWVOLTAGE_LOSS_RATE, YEARMONTH, LOWVOLTAGE_LOSS_RATE, NAME, YEARMONTH, PPQ, PPQ, SPQ, PPQ, PPQ, SPQ, CODE, issue "; pattern p = Pattern. compile ("(\ B [\ w | _] + \ B ,?) (. *) \ 1 (. *) "); Matcher matcher = p. matcher (s); while (matcher. find () {s = matcher. replaceAll ("$1 $2 $3"); System. out. println (s); matcher = p. matcher (s);} if (s. endsWith (",") s = s. substring (0, s. length ()-1); String round Ct = "LOWVOLTAGE_LOSS_RATE, YEARMONTH, NAME, PPQ, SPQ, CODE"; Assert. assertEquals (exact CT, s. substring (0, s. length ()-1); the verification result is the expected result. I thought it was over now, but after the system was running for a while, an error was reported. The trace found that when the string to be processed is: "D_NAME, COMPANYID, COMPANY_NAME, PARENT_ID, ID, COMPANYID, COMPANY_NAME, DF_UNIFIED_CODE, DF_CLEARANCE_TAG_ID, ", the result of the execution will replace" DF_CLEARANCE_TAG_ID "with" DF_CLEARANCE_TAG _ ", which is not what I want! The improved regular expression is: (\ B [\ w | _] + \ B ,?) (. *) \ B \ 1 \ B (. *). test again and the results fully meet the requirements.

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.