# Regexp help needed

**URL:** <https://www.ruby-forum.com/t/regexp-help-needed/136373>\
**Category:** Ruby\
**Created:** [May 1, 2008, 10:42am UTC](https://www.ruby-forum.com/t/regexp-help-needed/136373 "2008-05-01T10:42:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 1, 2008, 10:42am UTC](https://www.ruby-forum.com/t/regexp-help-needed/136373/1 "2008-05-01T10:42:17Z")

</div>

I have some text documents containing series of questions and answers.  
I need to extract all questions and answers to load a database.  
No problem in reading the text document or writing to the database.  
I need help with the regexps to parse the document (please note that  
some questions and answers extend on two or more lines).

This is an example of the documents I have to deal with:

\*1) Is this the first question ?  
a. Yes  
b. No

\*2) Is this question composed  
of two lines ?  
a. Yes, indeed  
b. Maybe  
c. I dont’ know

Many thanks in advance,  
Bruno

---

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 1, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/regexp-help-needed/136373/2 "2008-05-01T16:20:55Z")

</div>

On May 1, 2008, at 3:41 AM, Brubix wrote:

> I have some text documents containing series of questions and answers.  
> I need to extract all questions and answers to load a database.  
> No problem in reading the text document or writing to the database.  
> I need help with the regexps to parse the document (please note that  
> some questions and answers extend on two or more lines).

I would probably do it without leaning on regular expressions in this  
case:

#!/usr/bin/env ruby -wKU

DATA.each("") do |qna|  
answers = qna.to\_a  
question = answers.shift  
question \<\< answers.shift until question.strip[-1] == ??

```
 puts "Question: #{question}"
 puts "Answers:"
 puts answers

```

end

**END**  
\*1) Is this the first question ?  
a. Yes  
b. No

\*2) Is this question composed  
of two lines ?  
a. Yes, indeed  
b. Maybe  
c. I dont’ know

If you really want the regular expression though, this seems to work:

#!/usr/bin/env ruby -wKU

DATA.read.scan(/^(\*\d+) [\s\S]+??) \*\n((?:^[a-z]. .+?\n)+)/m) do  
|q, a|  
puts “Question: #{q}”  
puts “Answers:”  
puts a.to\_a  
end

**END**  
\*1) Is this the first question ?  
a. Yes  
b. No

\*2) Is this question composed  
of two lines ?  
a. Yes, indeed  
b. Maybe  
c. I dont’ know

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 1, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/regexp-help-needed/136373/3 "2008-05-01T16:36:09Z")

</div>

On May 1, 3:41 am, Brubix [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have some text documents containing series of questions and answers.  
> I need to extract all questions and answers to load a database.  
> No problem in reading the text document or writing to the database.  
> I need help with the regexps to parse the document (please note that  
> some questions and answers extend on two or more lines).
> 
> This is an example of the documents I have to deal with:

qa = "\*1) Is this the first question ?  
a. Yes  
b. No

\*2) Is this question composed  
of two lines ?  
a. Yes, indeed  
b. Maybe  
c. I dont’ know"

qa.scan(/(\*\d+)._??._?)([^_]_)/m).map do |q, ans|  
[q, ans.scan(/\n([a-z]..\*)/).flatten]  
end

=\> [[“\*1) Is this the first question ?”, [“a. Yes”, “b. No”]], [“\*2)  
Is this question composed\nof two lines ?”, [“a. Yes, indeed”, “b.  
Maybe”, "c. I dont’ know "]]]

That makes various assumptions about the appearance of ‘\*’ and how  
answers start/end, but it should be a start.

---

<div class="post-metadata">

**Author:** ![Brubix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@Brubix](https://www.ruby-forum.com/u/Brubix)\
**Post date:** [May 2, 2008, 9:22am UTC](https://www.ruby-forum.com/t/regexp-help-needed/136373/4 "2008-05-02T09:22:22Z")

</div>

All the proposed solutions work perfectly !

Thanks to both of you.
