Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby Regexp vs Perl and C#

Reply
Thread Tools

Ruby Regexp vs Perl and C#

 
 
Chris Meyers
Guest
Posts: n/a
 
      10-13-2006
I am having trouble with the Ruby regexp engine and don't know if it is
my lack of experience with Ruby, or if it just isn't possible to do
certain things with the Ruby engine. Basically I have the following
code in perl and C#, simplified for the example:

Perl:
$line = "banana";
while( $line =~ /(an)*/g )
{
print $`. "<<" . $& . ">>" . $' . "\n";
}

C#:
using System;
using System.Text.RegularExpressions;

public class MyClass
{
public static void Main()
{
string testString = "banana";
MatchCollection matches = Regex.Matches( testString, "(an)*" );
foreach( Match match in matches)
{
Console.WriteLine( testString.Substring( 0, match.Index) +
"<<" + match.Value + ">>" + testString.Substring( match.Index +
match.Length));
}
}
}

Output from both:
<<>>banana
b<<anan>>a
banan<<>>a
banana<<>>

While the C# way of getting the answer is a bit more messy, it is still
possible. I am wondering if/how to do this in Ruby. From what I have
seen, all the Ruby options are a single match which doesn't help me,
especially with the given example where Ruby's only match would be the
one before the string thus being empty. Using the string#scan in ruby
also doesn't help as it gives a 2 dimensional array of the following:
Ruby:
irb(main):001:0> line = "banana"
=> "banana"
irb(main):002:0> line.scan(/(an)*/)
=> [[nil], ["an"], [nil], [nil]]

So while Ruby finds that there should be 4 matches, it returns them in a
difficult to use format, with no index information, and matches only
"an" rather than "anan" as I want.

So basically is there a way to do this in Ruby? Which can also be asked
as, how do you do multiple matches in Ruby with full match information?

Thanks,
Chris

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
matt neuburg
Guest
Posts: n/a
 
      10-13-2006
Chris Meyers <> wrote:

> how do you do multiple matches in Ruby with full match information?


gsub

m.

--
matt neuburg, phd = , http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
 
 
 
matt neuburg
Guest
Posts: n/a
 
      10-13-2006
matt neuburg <> wrote:

> Chris Meyers <> wrote:
>
> > how do you do multiple matches in Ruby with full match information?

>
> gsub


In fact you could effectively write it exactly as you did it in Perl:

"banana".gsub(/(an)*/) {
puts $` + "<<" + $& + ">>" + $'
}

m.



--
matt neuburg, phd = , http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      10-13-2006
On Oct 12, 2006, at 7:00 PM, Chris Meyers wrote:

> I am having trouble with the Ruby regexp engine and don't know if
> it is
> my lack of experience with Ruby, or if it just isn't possible to do
> certain things with the Ruby engine. Basically I have the following
> code in perl and C#, simplified for the example:
>
> Perl:
> $line = "banana";
> while( $line =~ /(an)*/g )
> {
> print $`. "<<" . $& . ">>" . $' . "\n";
> }


scan() can also take a block:

"banana".scan { puts "#{$`}<<#{$&}>>#{$'}" }

James Edward Gray II

 
Reply With Quote
 
Verno Miller
Guest
Posts: n/a
 
      10-15-2006
> Chris Meyers wrote:
> ...
> ... and [Ruby] matches only "an" rather than "anan" as I want.
>
> So basically is there a way to do this in Ruby? Which can also be asked
> as, how do you do multiple matches in Ruby with full match information?
>
> Thanks,
> Chris



It's a bit tricky but still doable!

line = "banana"

If your goal is to match just (an)* in "banana" use:

line.scan(/((an)*)/) { |str|
#puts $1.inspect
puts $1 if $1 != ""
}


To match surrounding characters as well use:

line.scan(/([^a]|a(?=[^n]|$)|(an)*)/) { |str|
puts $1.inspect unless $1.empty?
}


Additional characters can be inserted this way:

line = "bananaxyzantuia"

str = ""

line.scan(/([^a]|a(?=[^n]|$)|(an)*)/) {

match = $1

puts match if match =~ /^[^a]$/
puts match if match =~ /^a$/
puts match if match =~ /^(an)+$/

if match =~ /^[^a]$/ then str << match
elsif match =~ /^a$/ then str << match
elsif match =~ /^(an)+$/ then str << "<<" << match << ">>"
end

}

puts str #=> b<<anan>>axyz<<an>>tuia


Instead of if-elsif-end you can also use case-when-end of course (cf.
http://www.bigbold.com/snippets/posts/show/1313 ).


Cheers,
verno


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Chris Meyers
Guest
Posts: n/a
 
      10-16-2006
Verno Miller wrote:
>
> It's a bit tricky but still doable!
>
> line = "banana"
>
> If your goal is to match just (an)* in "banana" use:
>
> line.scan(/((an)*)/) { |str|
> #puts $1.inspect
> puts $1 if $1 != ""
> }


Thank you Verno, that is what I needed. By using the block syntax I am
able to get the same output as Perl or C# and I can adapt that to my
problem at hand. For anyone interested, to recreate the same output in
Ruby as my Perl and C# example the following code works:

line="banana"
line.scan(/(an)*/) {
puts $` + "<<" + $& + ">>" + $' + "\n"
}

Thanks again, Chris

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      10-17-2006
On 10/16/06, Chris Meyers <> wrote:
>
> line="banana"
> line.scan(/(an)*/) {
> puts $` + "<<" + $& + ">>" + $' + "\n"
> }


Also, if you do a

require 'English'

you can write that as

puts $PREMATCH + "<<" + $MATCH + ">>" + $POSTMATCH

(you don't need the explicit \n if you're using puts)

martin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:50 PM
regexp problem - differences in Perl and Ruby Sam Dela Cruz Ruby 3 01-10-2006 02:08 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57