• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pattern Matching

 
Shan Pyong
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works, but I'm not sure if there's an easier way.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!