Matching multiple line reg exp

G

Guillermo Riojas

Hi there,
Trying for a couple of hours (again without success) to create a regular
expression that matches a multi-line statement , i found myself here
again, asking for help.
The case is now:

<def>/123/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<nop>xxxxx
<tuv>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<wxy>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx

I am unable to match the statements that contains multiple lines.
The regexp looks so far like this:

/(\<[a-z][a-z][a-z]\>.*?$)/m

The question is why is only returning matches until the first end of
line?
I am sure the greediness of the expression should take him to the last
new line character just before the next statement comes, but maybe is
just that i need to read a lot more to understand the concept right.
In any case, can somebody point me what i am missing to run the scan
method and get each match (including the ones with multiple lines)?

Thanks a lot.
 
W

w_a_x_man

Hi there,
Trying for a couple of hours (again without success) to create a regular
expression that matches a multi-line statement , i found myself here
again, asking for help.
The case is now:

<def>/123/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<nop>xxxxx
<tuv>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<wxy>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx

I am unable to match the statements that contains multiple lines.
The regexp looks so far like this:

/(\<[a-z][a-z][a-z]\>.*?$)/m

The question is why is only returning matches until the first end of
line?
I am sure the greediness of the expression should take him to the last
new line character just before the next statement comes, but maybe is
just that i need to read a lot more to understand the concept right.
In any case, can somebody point me what i am missing to run the scan
method and get each match (including the ones with multiple lines)?

Thanks a lot.

/^(<[a-z]{3}>[^<]*)/
 
N

Nathan Clark

Hi there,
Trying for a couple of hours (again without success) to create a regular
expression that matches a multi-line statement , i found myself here
again, asking for help.
The case is now:

<def>/123/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<nop>xxxxx
<tuv>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<wxy>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx

I am unable to match the statements that contains multiple lines.
The regexp looks so far like this:

/(\<[a-z][a-z][a-z]\>.*?$)/m

The question is why is only returning matches until the first end of
line?
I am sure the greediness of the expression should take him to the last
new line character just before the next statement comes, but maybe is
just that i need to read a lot more to understand the concept right.
In any case, can somebody point me what i am missing to run the scan
method and get each match (including the ones with multiple lines)?

Thanks a lot.

/^(<[a-z]{3}>[^<]*)/

In multiline regular expressions in Ruby, the caret and dollar sign
also match newlines in the string, which is why it only read a line at
a time.
 
J

Josh Cheek

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

On Mon, Nov 22, 2010 at 2:33 PM, Guillermo Riojas <
Hi there,
Trying for a couple of hours (again without success) to create a regular
expression that matches a multi-line statement , i found myself here
again, asking for help.
The case is now:

<def>/123/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<nop>xxxxx
<tuv>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<wxy>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx

I am unable to match the statements that contains multiple lines.
The regexp looks so far like this:

/(\<[a-z][a-z][a-z]\>.*?$)/m

The question is why is only returning matches until the first end of
line?
I am sure the greediness of the expression should take him to the last
new line character just before the next statement comes, but maybe is
just that i need to read a lot more to understand the concept right.
In any case, can somebody point me what i am missing to run the scan
method and get each match (including the ones with multiple lines)?

Thanks a lot.
Hi, Guillermo, your regexp works for me. Perhaps you are using a method that
only looks for the first match.





str = "<def>/123/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<nop>xxxxx
<tuv>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
<wxy>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx"

results = str.scan( /(\<[a-z][a-z][a-z]\>.*?$)/m )

require 'pp'
pp results

# >> [["<def>/123/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"],
# >> ["<nop>xxxxx"],
# >> ["<tuv>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"],
# >> ["<wxy>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]]
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top