Help with regex

L

Lucas Fialho

[Note: parts of this message were removed to make it a legal post.]

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' ?
 
L

Louis-Philippe

[Note: parts of this message were removed to make it a legal post.]

try putting word metacharacter instead of an any metacharacter

\w*

instead of

*
 
B

Benoit Daloze

[Note: parts of this message were removed to make it a legal post.]

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.
 
L

Lucas Fialho

[Note: parts of this message were removed to make it a legal post.]

thanks guys. I did not know about string scan!

2009/11/26 Benoit Daloze said:
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 Fialho said:
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' ?
 
R

Robert Klemme

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top