embedded code construct a la perl

V

Vince Forgetta

Hi all,

Is it possible to use embedded-code constructs in ruby regexp?

Perl Example:

<code>

"123"= ~ m{
\d+
(?{print "matched at [$'<$&>$']\n"})
(?!)
}x;

</code>

Output:

matched at [<123>]
matched at [<12>3]
matched at [<1>23]
matched at [1<23>]
matched at [1<2>3]
matched at [12<3>]



Thanks for the help

--
Vince Forgetta

Computational Biology
McGill University and Genome Quebec Innovation Centre
740 Dr. Penfield Avenue Room 7211
Montreal, Quebec Canada, H3A 1A4

Tel: 514-398-3311 00476
Email: (e-mail address removed)
 
R

Robert Klemme

Vince Forgetta said:
Hi all,

Is it possible to use embedded-code constructs in ruby regexp?

Perl Example:

<code>

"123"= ~ m{

I guess, this should have read
"123" =~ m{
\d+
(?{print "matched at [$'<$&>$']\n"})
(?!)
}x;

</code>

Output:

matched at [<123>]
matched at [<12>3]
matched at [<1>23]
matched at [1<23>]
matched at [1<2>3]
matched at [12<3>]

No. If you let us know what you want to do mayve we can come up with an
alternative solution.

Regards

robert
 
V

Vince Forgetta

I want to find all nested pattern matches in a string, and execute some
code at each match. This should include searching all nested substrings
of a match. I realize I could do this in perl ( I have been up to now),
but my code is getting quite complex and I would like to implement an
object model (something I could also do in perl, but works much better
in Ruby). In perl, I was able to do this in a regexp (example below).
Can I do the same thing in ruby, or will I need to use some sort of loop
to wun code at each match.

Thanks.

Vince


Robert said:
Hi all,

Is it possible to use embedded-code constructs in ruby regexp?

Perl Example:

<code>

"123"= ~ m{

I guess, this should have read
"123" =~ m{


\d+
(?{print "matched at [$'<$&>$']\n"})
(?!)
}x;

</code>

Output:

matched at [<123>]
matched at [<12>3]
matched at [<1>23]
matched at [1<23>]
matched at [1<2>3]
matched at [12<3>]

No. If you let us know what you want to do mayve we can come up with an
alternative solution.

Regards

robert


--
Vincenzo Forgetta

Computational Biology
McGill University and Genome Quebec Innovation Centre
740 Dr. Penfield Avenue Room 7211
Montreal, Quebec Canada, H3A 1A4

Tel: 514-398-3311 00476
Email: (e-mail address removed)
 
R

Robert Klemme

Vince Forgetta said:
I want to find all nested pattern matches in a string, and execute some
code at each match. This should include searching all nested substrings
of a match. I realize I could do this in perl ( I have been up to now),
but my code is getting quite complex and I would like to implement an
object model (something I could also do in perl, but works much better
in Ruby). In perl, I was able to do this in a regexp (example below).
Can I do the same thing in ruby, or will I need to use some sort of loop
to wun code at each match.

As said, no code in Ruby regexps. You'll have to do looping.

str.scan rx do |m|
doForAllSubstringsOf m do |subString|
puts subString
end
end

The funny part is implementing doForAllSubstringsOf(str). :) Well, it
isn't really difficult:

def doForAllSubstringsOf(str)
len = str.length

for i in 0 ... len
for j in 1 ... len - i
yield str[i, j]
end
end
end


Regards

robert
Thanks.

Vince


Robert said:
Hi all,

Is it possible to use embedded-code constructs in ruby regexp?

Perl Example:

<code>

"123"= ~ m{

I guess, this should have read
"123" =~ m{


\d+
(?{print "matched at [$'<$&>$']\n"})
(?!)
}x;

</code>

Output:

matched at [<123>]
matched at [<12>3]
matched at [<1>23]
matched at [1<23>]
matched at [1<2>3]
matched at [12<3>]

No. If you let us know what you want to do mayve we can come up with an
alternative solution.

Regards

robert


--
Vincenzo Forgetta

Computational Biology
McGill University and Genome Quebec Innovation Centre
740 Dr. Penfield Avenue Room 7211
Montreal, Quebec Canada, H3A 1A4

Tel: 514-398-3311 00476
Email: (e-mail address removed)
 
V

Vince Forgetta

Thank you.

Vince

Robert said:
I want to find all nested pattern matches in a string, and execute some
code at each match. This should include searching all nested substrings
of a match. I realize I could do this in perl ( I have been up to now),
but my code is getting quite complex and I would like to implement an
object model (something I could also do in perl, but works much better
in Ruby). In perl, I was able to do this in a regexp (example below).
Can I do the same thing in ruby, or will I need to use some sort of loop
to wun code at each match.

As said, no code in Ruby regexps. You'll have to do looping.

str.scan rx do |m|
doForAllSubstringsOf m do |subString|
puts subString
end
end

The funny part is implementing doForAllSubstringsOf(str). :) Well, it
isn't really difficult:

def doForAllSubstringsOf(str)
len = str.length

for i in 0 ... len
for j in 1 ... len - i
yield str[i, j]
end
end
end


Regards

robert


Thanks.

Vince


Robert Klemme wrote:


Newsbeitrag

Hi all,

Is it possible to use embedded-code constructs in ruby regexp?

Perl Example:

<code>

"123"= ~ m{




I guess, this should have read
"123" =~ m{





\d+
(?{print "matched at [$'<$&>$']\n"})
(?!)
}x;

</code>

Output:

matched at [<123>]
matched at [<12>3]
matched at [<1>23]
matched at [1<23>]
matched at [1<2>3]
matched at [12<3>]




No. If you let us know what you want to do mayve we can come up with
an

alternative solution.

Regards

robert
--
Vincenzo Forgetta

Computational Biology
McGill University and Genome Quebec Innovation Centre
740 Dr. Penfield Avenue Room 7211
Montreal, Quebec Canada, H3A 1A4

Tel: 514-398-3311 00476
Email: (e-mail address removed)


--
Vincenzo Forgetta

Computational Biology
McGill University and Genome Quebec Innovation Centre
740 Dr. Penfield Avenue Room 7211
Montreal, Quebec Canada, H3A 1A4

Tel: 514-398-3311 00476
Email: (e-mail address removed)
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top