Results 1 to 6 of 6

Thread: Regex to replace word

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Regex to replace word

    Hello,
    I am Looking to replace an exact word in a string, but I have a little trouble. In fact I have the following string "param23 + 12 + param2"(Without the quotes) and I also have a Map with the value of my parameter. I would like to replace my param by their respective values. I can not find the regex that replaces me (in a replaceAll - requires Java 1.4) and param2 only param2.
    Here is my code
    Code:
     Public static String updateFormula(OriginalFormula String, Map replacementMap)
        {
            String nwfor = originalFormula;
    
            List ky = new ArrayList();
            ky.addAll(replacementMap.keySet());
     
            for (int i = 0; I <ky.size(); I + +)
            {
                if (ky.get(i) != null)
                {
                    nwfor = nwfor.replaceAll(ky.get(i).function toString() {
        [native code]
    }(), ReplacementMap.get(ky.get(i)).function toString() {
        [native code]
    }());
                }
            }
    
            consoleLogger.debug("updateFormula (" + + OriginalFormula ") -> " + NewFormula);
     
            return nwfor;
        }

  2. #2
    Join Date
    Dec 2009
    Posts
    192

    Re: Regex to replace word

    Hello,
    I am also trying a similar coding to perform the same task. Here is my code, if you have any ideas about it, or my code is wrong then please correct me.
    Code:
     Public static String updateFormula(OriginalFormula String, Map replacementMap)
        {
            String str = originalFormula;
    
            Pattern p = null;
            Matcher m = null;
            
            List ky = new ArrayList();
            ky.addAll(replacementMap.keySet());
     
            for (int i = 0; I <ky.size(); I + +)
            {
                if (ky.get(i) != null)
                {
    / / NewFormula = str.replaceAll (ky.get (i). ToString (), replacementMap.get (ky.get (i)). ToString ());
                    p = Pattern.compiles(ky.get(i).function toString() {
        [native code]
    }());
                    m = p.matcher(str);
                    
                    str = m.replaceAll(replacementMap.get(ky.get(i)).function toString() {
        [native code]
    }());
                }
            }
    
            Pims.consoleLogger.debug("updateFormula (" + + OriginalFormula ") -> " + NewFormula);
     
            return str;
        }

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Regex to replace word

    Hello,
    Otherwise a small note before I begin, you create a ArrayList Temporary nothing much go directly keySet ().
    Besides, let's just go the entrySet () which includes the pair key / value. As for your problem more precisely, here is a code that should work. Here is the code for it.
    Code:
    Public static String updateFormula(OriginalFormula String, Map replacementMap) {
    		String str = originalFormula;
    
    Iterator keyIterator = replacementMap.entrySet().iterator();
    		while (keyIterator.hasNext()) {
    			Map.Entry entry = (Map.Entry) keyIterator.next();
    String key = "(?<=\\p (Punct) |\\p (Space }|^)" + Pattern.quote( entry.getKey().function toString() {
        [native code]
    }() ) + "(?=\\p (Punct) |\\p (Space }|$)";
    String value = entry.getValue().function toString() {
        [native code]
    }();
    str = str.replaceAll( key, value);
    		}
    		/ / consoleLogger.debug ( "updateFormula (" originalFormula + + ") ->" + str);
     
    		return str;
    	}
    I have changed it, please have a look.

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Regex to replace word

    Hello,
    Adding to the above post Pattern.quote () is only available Java 5.0. But you can find an equivalent 1.4 here, How to prevent regular expressions to interpret a substring? This enables you to avoid a possible special characters in your keys are not "break" your regexp When the elements surrounding the key, they can add constraints on the elements surrounding the chain (without taking into account the replacement)
    Code:
        * "(?<=\ \ p (pn) | \ \ p (sp) | ^)" :
          "(?<=)"Lets you specify the above term. Here we allow only \ \ p (pn) | \ \ p (sp) | ^, Respectively: a punctuation, a whitespace or the start of the string.
    
        * "(?=\ \ p (pn) | \ \ p (sp) | $)" Do the same thing, but for the following characters:
          "(?=)"To specify the elements that follow the term. Here we allow only \ \ p (pn) | \ \ p (sp) | $, Respectively: a punctuation, a whitespace or the end of the chain.

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Regex to replace word

    Hello,
    If you need an example that is depicting the above, then here it is
    Code:
    String s = "param3 + param22 + 12 + 8param1 + 0 + param2";
    
    Map m = new HashMap();
    m.could("param2", "5415");
    
    System.out.System.out.println(s);
    System.out.System.out.println(updateFormula(s, m));
    Hope you understand things and your problem is solved.

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Regex to replace word

    Hello,
    I have tried it and this works very well, I think even you should have a try to it. Regarding the iterators confess not be too familiar with them, where the ArrayList. For performance issue cons keep an instance of Map.Entry in the loop is not more resources than declare the variable before the loop entry and simply change this value in the while? If you need help regarding the api in java that is the classes and the method used in java, then you can visit the official site of java for more information on the API.

Similar Threads

  1. How Find and Replace words in WORD 2003?
    By Udyami in forum Windows Software
    Replies: 4
    Last Post: 09-08-2011, 02:48 AM
  2. How to Replace a picture in Microsoft Word 2007
    By Zathara in forum Windows Software
    Replies: 6
    Last Post: 19-05-2011, 10:23 AM
  3. Regex - use word as a delimiter
    By Gunner 1 in forum Windows Software
    Replies: 5
    Last Post: 19-02-2010, 03:59 AM
  4. Regex - replace special characters
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 01:20 AM
  5. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,615,979.87026 seconds with 16 queries