# Question concerning multiline regexps and best practice

**URL:** <https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618>\
**Category:** Ruby\
**Created:** [December 6, 2005, 11:48pm UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618 "2005-12-06T23:48:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 6, 2005, 11:48pm UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/1 "2005-12-06T23:48:55Z")

</div>

Hi,

I am currently moving an application from Python to Ruby for a training  
purpose and to learn Ruby. Inside this application I am parsing text  
files delivered by news agencies. These follow more or less a  
specification developed by the IPTC consortium. But back to the  
question. 🙂

At the momemt I use a concatenation of strings as an input for the  
Regexp, but I asked myself wether I can use a HERE document, as it would  
make things a lot clearer without all the these single and double  
quotes around strings. But sadly inside a HERE document, the \n at the  
end of a line are used by the regexp. Is it possible to write a HERE  
document or something like that with \n inside, but afterwards the \n  
the source are skipped?

Or may be there is an even better way to do it. I even think about  
writing a bunch of methods to parse all the stuff without a regex.

Best regards,  
Oliver

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 6, 2005, 11:52pm UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/2 "2005-12-06T23:52:58Z")

</div>

unless i do not understand the question, a regex option that allows  
multiline matches might be used.

konstantin

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 12:09am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/3 "2005-12-07T00:09:12Z")

</div>

ako… schrieb:

> unless i do not understand the question, a regex option that allows  
> multiline matches might be used.

Well, to better describe what I am currently dealing with, I post a  
snippet of python code with the regex.

msg\_rx = re.compile(  
“^\x01?” +  
"(?P[a-zA-Z]{3,4})(?P\d{3,4}) " +  
"(?P\d) " +  
"(?P[a-zA-Z]{1,3}) " +  
"(?P\d{1,4}) " +  
“(?P._)\r\n_” +  
“(?P._)\r\n_” +  
“\x02” +  
“(?:(?P._)=\s_\r\n)?” +  
“(?P._)" +  
"\x03._” +  
"(?P\d{2})(?P\d{2})(?P\d{2}) " +  
"(?P[a-zA-Z]{3}) " +  
“(?P\d{2})”,  
re.S  
)

This little “baby” does the job. As ruby doesn’t have named groups in  
regexps, I have to add comment lines (?#…) to document the invidual  
groups. This would glutter the thing even more. Now ruby has these nice  
HERE documents, %r{…} and so on. I would be happy if I could achieve  
something like that.

msg\_rx = %r{  
^\x01?  
(?# comment for the line)  
([a-zA-Z]{3,4})(?P\d{3,4})\s  
(\d)\s  
(?# comment for the line)  
([a-zA-Z]{1,3})\s  
(?# comment for the line)  
(\d{1,4})\s  
(._)\r\n_  
(._)\r\n_  
\x02  
(?# comment for the line)  
(?:(._)=\s_\r\n)?  
(?# comment for the line)  
(._)  
\x03._  
(?# comment for the line)  
(\d{2})(\d{2})(\d{2})\s  
(?# comment for the line)  
([a-zA-Z]{3})\s  
(?# comment for the line)  
(\d{2})  
}

Thinks looks a lot cleaner for me, but sadly the “\n” at the end of the  
lines are in my way. 🙂 I could strip them, but if it would just  
“happen” it would be nicer.

Hopefully, this makes my question a little clearer.

Best regards, Oliver

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 12:33am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/4 "2005-12-07T00:33:41Z")

</div>

the best that i could come up with is to remove new lines from your  
regexp:

(ms\_rx = \<\<END).gsub!(/\n/, ‘’)  
line one  
line two  
END

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 1:10am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/5 "2005-12-07T01:10:14Z")

</div>

the best that i could come up with:

(var = \<\<TARGET).gsub!(/\n/, ‘’)  
line one  
line two  
TARGET

puts var

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 12:21am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/6 "2005-12-07T00:21:27Z")

</div>

On Wed, Dec 07, 2005 at 07:47:34AM +0900, Oliver A. wrote:

> But sadly inside a HERE document, the \n at the end of a line are  
> used by the regexp. Is it possible to write a HERE document or  
> something like that with \n inside, but afterwards the \n the source  
> are skipped?

The cleanest solution is to make a regular expression that can work  
regardless of the presence of newlines. You probably want multiline  
mode. I’d need to see your specific example.

Or you could strip out the newlines with mystring.sub("\n","").

regards,  
Ed

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 1:46am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/7 "2005-12-07T01:46:44Z")

</div>

Oliver A. ha scritto:

> ako… schrieb:

> Thinks looks a lot cleaner for me, but sadly the “\n” at the end of the  
> lines are in my way. 🙂 I could strip them, but if it would just  
> “happen” it would be nicer.

use a /x swicth, it should work even with %r stuff:

> > rgx=%r[  
> > foo #foo  
> > bar #bar  
> > ]x  
> > =\> /  
> > foo #foo  
> > bar #bar  
> > /x
> 
> > m=rgx.match “foobar”  
> > =\> #MatchData:0x29c9490
> 
> > m[0]  
> > =\> “foobar”

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 2:15am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/8 "2005-12-07T02:15:06Z")

</div>

Oliver A. wrote:

> (\d)\s  
> (._)  
> \x03._  
> (?# comment for the line)  
> (\d{2})(\d{2})(\d{2})\s  
> (?# comment for the line)  
> ([a-zA-Z]{3})\s  
> (?# comment for the line)  
> (\d{2})  
> }

Use extended mode:

msg\_rx = %r{  
^\x01?

# comment for the line

([a-zA-Z]{3,4}) (\d{3,4}) \s  
(\d) \s  
}x

---

<div class="post-metadata">

**Author:** ![oliver.andrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/8e7dd6/32.png) [@oliver.andrich](https://www.ruby-forum.com/u/oliver.andrich)\
**Post date:** [December 7, 2005, 8:20am UTC](https://www.ruby-forum.com/t/question-concerning-multiline-regexps-and-best-practice/49618/9 "2005-12-07T08:20:27Z")

</div>

Thank William and Gabriele! This is exactly what I have been looking  
for. Now this part of my module looks nice, clean and uncluttered.

Best regards,  
Oliver
