• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Confused with String replace in strings

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm working on a code where I need to find a string from a set of sentences, match if the string is in another keywords list and from there get the appropriate value from map and replace in original list.

Here is my code



When I run this code the output that I get is as below.

Original strings: [bread gingerbread vegan, iced tea lmn gngr grn 32, iced tea lmn]
New strings: [breadd gingerbreadd vegetarian, iced tea lemon ginger green 32, iced tea lemon]



but my expected output is as below

Original strings: [bread gingerbread vegan, iced tea lmn gngr grn 32, iced tea lmn]
New strings: [bread gingerbread vegetarian, iced tea lemon ginger green 32, iced tea lemon]



Here I want to find entire match in rawTitleList from dictKeys and replace the value using the value from dictMap.

please let me know where am I going wrong and how can I fix this.

Thanks,
Sunny
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that both bread and gingerbread contain brea. What you seem to want is to use either whole words, or words ending with those. That means you need to use the word boundary - \b. Add that after and (if needed) before the current pattern. Note that your existing pattern needs to be enclosed with ( or (?: and ), otherwise the word boundaries will be part of the first and last "word".
 
Saloon Keeper
Posts: 9348
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Produces the pattern

Note the map(Pattern::quote) is unnecessary unless there's a small chance that a dictMap key contains special characters.
 
Carey Brown
Saloon Keeper
Posts: 9348
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your approach is ok for a small dictionary but this would be a more generalized approach that could handle a potentially large dictionary.
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:


Collectors.joining is overloaded to take a prefix and postfix:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic