0

I have this set (number of points variable, here 2):

A 
 This is some Text belonging to A
 This also belongs to A
B 
 This should be with B
 same with this
...
...

I want it to finally be a string like that:

A This is some Text belonging to A This also belongs to A
B This should be with B same with this

My try was something like:

answer.scan(/^([A-Z].+?(?=^[A-Z]))/m).map { |d| d.delete("\n") }.join("\n")

The problem is that this doesn't match the last set (you can assume the string ends with the last set) any ideas? :)

edit1: fixed a coyping error & tried a new regex in Rubular which kind of works but still has some unnecessary matches?

3
  • 2
    I'll suggest rubular.com - a regexp tester for Ruby! Commented Dec 5, 2012 at 9:49
  • 1
    Maybe regex isn't the solution to your problem Commented Dec 5, 2012 at 9:52
  • @ nikola. most questions didnt really get "good" or productive answers except maybe the Backbone one, which I abandoned since I stopped that project. Therefore I can't really say if its correct or not. (And the Question concerning tabs probably didn't belong here anyhow) Commented Dec 5, 2012 at 9:58

2 Answers 2

4
text = <<EOS
A 
 This is some Text belonging to A
 This also belongs to A
B 
 This should be with B
 same with this
 variable line
EOS

text.gsub(/\s?\n\s/, ' ')

# Outputs: 
# A This is some Text belonging to A This also belongs to A
# B This should be with B same with this variable line
Sign up to request clarification or add additional context in comments.

2 Comments

think that would only work for 2 sets? with 3 it doesnt seem to insert a newline between 2 and 3
with set I mean an upper case letter and then some text. have managed to do it now, although it seems a bit messy...
1
answer = answer
.scan(/^([A-Z].+?(?=^[A-Z]))|(^[A-Z].+?\Z)/m)
.map {|item| item.reject { |i| i.nil? } }
.map { |d| d[0].delete("\n") }
.join("\n")

seems to work for now... probably not the best possible way though

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.