# Help with regex

**URL:** <https://www.ruby-forum.com/t/help-with-regex/179522>\
**Category:** Ruby\
**Created:** [November 26, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/help-with-regex/179522 "2009-11-26T22:31:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [November 26, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/help-with-regex/179522/1 "2009-11-26T22:31:34Z")

</div>

Hello everyone.  
I’m trying to match the following pattern:

Text1\n  
Text2\n  
Text3—

Please note that Text3 can contain \n.  
I tried using this regex:

/(._?)\n(._?)\n(.\*?)—/

The pattern matches and I have three matches, but they’re like this:

match[0] =\> “Text2\nText3—”  
macth[1] =\> ‘Text2’  
match[2] =\> ‘Text3’

How can I get the first one to be ‘Text1’ ?

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [November 26, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/help-with-regex/179522/2 "2009-11-26T22:52:03Z")

</div>

try putting word metacharacter instead of an any metacharacter

\w\*

instead of

.\*

2009/11/26 Lucas F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [November 26, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/help-with-regex/179522/3 "2009-11-26T22:56:09Z")

</div>

s = “Text1\n  
Text2\n  
Text3—”

irb(main):036:0\> s.scan( /(\w+?)(?:\n|-+)/ )  
=\> [[“Text1”], [“Text2”], [“Text3”]]

Usually, it’s easier to use scan when you feel your code in the Regexp  
gonna  
be repeated.  
If the text is so simple, you could iterate which String#each\_line, and  
then  
remove any ‘-’ at the end.

2009/11/26 Lucas F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [November 27, 2009, 8:18am UTC](https://www.ruby-forum.com/t/help-with-regex/179522/4 "2009-11-27T08:18:34Z")

</div>

On 11/27/2009 02:37 AM, Lucas F. wrote:

> [Note: parts of this message were removed to make it a legal post.]
> 
> thanks guys. I did not know about string scan!

If you know there are always three groups you can also do:

irb(main):006:0\> s=“t1  
irb(main):007:0” t2  
irb(main):008:0" t3—"  
=\> “t1\nt2\nt3—”  
irb(main):009:0\> /([^\n]+)\n([^\n]+)\n([^\n]+)—/ =~ s  
=\> 0  
irb(main):010:0\> $1  
=\> “t1”  
irb(main):011:0\> $2  
=\> “t2”  
irb(main):012:0\> $3  
=\> “t3”

Cheers

robert

---

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [November 27, 2009, 2:38am UTC](https://www.ruby-forum.com/t/help-with-regex/179522/5 "2009-11-27T02:38:31Z")

</div>

thanks guys. I did not know about string scan!

2009/11/26 Benoit D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
