• 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

Pattern Matching

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

I have a string like this:
String s="Cat and or dog and and cow or or or and sheep";

Expected string:
String result="Cat and dog and cow or sheep";

I would like to replace patterns like "and and and..." by a single "and" (same for "or"). Similarly, if there is a pattern like "and or.." or "or and or.." in the string, I want that to be replaced with the first word in the pattern. That is, by "and" , "or" respectively.

String s="Cat and or dog";
Output: Cat and dog
String s="cat or and and dog";
Output: cat or dog

Please let me know how to do handle this. Many thanks for your help.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This works, but I'm not sure if there's an easier way.
 
reply
    Bookmark Topic Watch Topic
  • New Topic