Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Confused with String replace in strings

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • 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: 22014
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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: 7381
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 7381
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 22014
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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