Java Regular Expression for Dynamic Replacement of strings

Source: Internet
Author: User

Java Regular Expression for Dynamic Replacement of strings
Scenario: when processing SQL statements today, because the format stored in the database is VARCHAR2, a single quotation mark must be added to the string with digits following the WHERE condition in the SQL statement, the first thing that comes to mind for string processing is regular expressions. Although I have some knowledge about regular expressions, I am also very familiar with it, baidu (the normal network is used by some helpless search engines). It is found that replacement is basically a fixed replacement, that is, to conform to a certain type of Regular Expression standards, replace all the strings with a fixed string. For example, it is easy to process typos, which is also implemented in the program. However, I need multiple dynamic replicas. The most basic thing is to keep the original matching string and then add something at the beginning and end. I searched for it and did not find one that met my requirements. Well, let's do it by yourself. Solution: Let me briefly talk about my ideas. I split the string twice, matching the string that meets the regular expression at a time, and matching the string that does not meet the regular expression at a time, in this way, they always appear, so that I can operate on matching strings or non-matching strings at will, and then let them together. Source code: Copy code 1 package com. util. regex; 2 3 import java. util. iterator; 4 import java. util. linkedHashMap; 5 import java. util. map; 6 import java. util. regex. matcher; 7 import java. util. regex. pattern; 8 9 public class RegexUtil {10 private String strSource = ""; 11 private String StrRe = ""; 12 13 public String getStrSource () {14 return strSource; 15} 16 public void setStrSource (String strSource) {17 this. strSou Rce = strSource; 18} 19 public String getStrRe () {20 return StrRe; 21} 22 public void setStrRe (String strRe) {23 StrRe = strRe; 24} 25 public String flixedReplace (String rep) {26 return this. strSource. replaceAll (this. strRe, rep); 27} 28/** 29 * <li> Description: </li> 30 * <li> Date: </li> 31 * <li> Modify: </li> 32 * <li> Version: 1.0 </li> 33 * @ author Administrator34 * @ param form35 * @ param last36 * @ return St Ring37 */38 @ SuppressWarnings ("unchecked") 39 public String splitReplace (String form, String last) {40 String ss [] = this. getStrSource (). split (this. getStrRe (); 41 Pattern p = Pattern. compile (this. getStrRe (); 42 Matcher m = p. matcher (this. getStrSource (); 43 LinkedHashMap <Integer, String> map = new LinkedHashMap <Integer, String> (); 44 int I = 0; 45 while (m. find () {46 map. put (I, form + m. group () + last); 47 I ++; 48} 4 9 Iterator <?> Iter = map. entrySet (). iterator (); 50 StringBuffer tarStr = new StringBuffer (); 51 for (int j = 0; j <ss. length; j ++) {52 if (! This. getStrSource (). isEmpty ()&&(! Character. isDigit (this. getStrSource (). charAt (0) | j! = 0) {53 tarStr. append (ss [j]); 54} 55 if (iter. hasNext () {56 Map. entry <Integer, String> entry = (Map. entry <Integer, String>) iter. next (); 57 if (entry. getKey () = j) {58 tarStr. append (entry. getValue (); 59} 60} 61} 62 return tarStr. toString (); 63} 64 65 public static void main (String [] args) {66 RegexUtil re = new RegexUtil (); 67 re. setStrSource ("2132 3213 adsd 12321 asdfsa dsdf 12 1313adfaf231321dfafda1141dfaffafdf"); 68 re. setStrRe ("\ d +"); 69 System. out. println (re. flixedReplace ("hello"); 70 System. out. println (re. splitReplace ("'", "'"); 71 // System. out. println (re. getStrSource (). replaceAll ("(\ w +) (\ d +)", "$1 '$2"); 72} 73}

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.