Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex,replaceAll with HashMap (Code Issue)

 
schgk khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2942
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2942
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch.
 
schgk khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2942
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2942
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Tookey wrote:
You should also make sure that nothing in the key is interpreted as a regex meta character by using

Good point.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic