1

str = "88Bifk8hB8BB8BBBB888chl8BhBfd" I would like to find all pairs in the string containing "B8" and "8B", they must be found and returned in order. str should return "8BB8B8B88B"

so far I have tried: str.scan("B8") which finds all the "B8" combinations, but this method doesn't allow for another argument for "8B". This might need a regex search but have tried some different combinations which either don't work or still just return a single combination, i.e. "B8".

sparkles
  • 23
  • 3
  • `"88Bifk8hB8BB8BBBB888chl8BhBfd".scan(/B8|8B/).join #=> "8BB8B8B88B"`. The question for which this question is claimed to be a duplicate does not appear to answer your question. I therefore am voting to reopen your question. – Cary Swoveland Feb 14 '21 at 21:49
  • I inadvertently downvoted your question. Please make a small edit so that I can correct my error and then delete this comment. Please also leave me a comment saying you've done so. – Cary Swoveland Feb 14 '21 at 21:53
  • @CarySwoveland Thank you for helping to reopen this! I was fairly sure the initial response didn't solve my question, but doubted myself as i'm very new to both stackoverflow and coding. This solution works great for me. Thanks all! – sparkles Feb 15 '21 at 22:49
  • You reached 15 points, now you can upvote questions and answers that you like. – Ryszard Czech Feb 23 '21 at 21:12

1 Answers1

1

Use

text.scan(/8B|B8/).join()

See regex proof and Ruby proof.

NODE EXPLANATION
8B '8B'
| OR
B8 'B8'
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37