• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex,replaceAll with HashMap (Code Issue)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch.
 
schgk khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic