wood burning stoves*
The moose likes Java in General and the fly likes Regex,replaceAll with HashMap (Code Issue) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regex,replaceAll with HashMap (Code Issue)" Watch "Regex,replaceAll with HashMap (Code Issue)" New topic
Author

Regex,replaceAll with HashMap (Code Issue)

schgk khan
Greenhorn

Joined: Jul 05, 2013
Posts: 3
Hello guys,

I have the following code and i want to match the text inside the body with my hashmap key and want to replace it with hashmap value, but this code is not working and not replacing at all. I am using negative lookbehind

Thanks in advance.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
That will look for and replace all occurrences of the hash key not preceded by '!' and replace them with the hash value - is that what you wanted to do?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
And welcome to the Ranch.
schgk khan
Greenhorn

Joined: Jul 05, 2013
Posts: 3
Tony Docherty wrote:That will look for and replace all occurrences of the hash key not preceded by '!' and replace them with the hash value - is that what you wanted to do?



well i have a what i want to do is i am replace #template in my string to some value which is in hashmap value for Instance in the scenario i have key = "#template", value "emailTemplate" i want to replace it in string using hashmap. thanks
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
schgk khan wrote:
Tony Docherty wrote:That will look for and replace all occurrences of the hash key not preceded by '!' and replace them with the hash value - is that what you wanted to do?


well i have a what i want to do is i am replace #template in my string to some value which is in hashmap value for Instance in the scenario i have key = "#template", value "emailTemplate" i want to replace it in string using hashmap. thanks

OK so why not just do a straight replaceAll, ie:

Or if you need to make sure you only match on whole words then restrict the match to word boundaries ie:

schgk khan
Greenhorn

Joined: Jul 05, 2013
Posts: 3
Tony Docherty wrote:
schgk khan wrote:
Tony Docherty wrote:That will look for and replace all occurrences of the hash key not preceded by '!' and replace them with the hash value - is that what you wanted to do?


well i have a what i want to do is i am replace #template in my string to some value which is in hashmap value for Instance in the scenario i have key = "#template", value "emailTemplate" i want to replace it in string using hashmap. thanks

OK so why not just do a straight replaceAll, ie:

Or if you need to make sure you only match on whole words then restrict the match to word boundaries ie:



Yeah thanks it works . I was missing out this part ", and thank you for your welcome greetings, I love being here.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

schgk khan wrote:

Yeah thanks it works . I was missing out this part ", and thank you for your welcome greetings, I love being here.


You should also make sure that nothing in the key is interpreted as a regex meta character by using

or

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2166
    
  47
Richard Tookey wrote:
You should also make sure that nothing in the key is interpreted as a regex meta character by using

Good point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex,replaceAll with HashMap (Code Issue)
 
Similar Threads
how to iterate a hashmap with key hashmap
how to seperate map values with same value ??
sysntex error in 2 dim array value assignment...
TypeCast Error while setting a value in a setter method
hashtable