Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regex in Ruby question

Reply
Thread Tools

Regex in Ruby question

 
 
J. Cooper
Guest
Posts: n/a
 
      02-22-2008
Another newbie question:

I've just started learning about regex "officially" (reading the
O'reilly book) and was trying out some of the examples (porting them to
Ruby from Perl). From what I can tell, Ruby's regex engine doesn't
support "lookbehind", and as I had only a smattering of knowledge about
regex before and no prior experience with lookaround, I was curious if
this was significant.

i.e. is there any problem that can't be solved without lookbehind?

(Note that I am not facing such a problem; I'm just curious if
lookbehind is just an optional feature that makes certain problems
easier, as opposed to being an essential thing.)

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

 
Reply With Quote
 
 
 
 
ThoML
Guest
Posts: n/a
 
      02-22-2008
> From what I can tell, Ruby's regex engine doesn't
> support "lookbehind"


Ruby 1.9 has look-behind:

(?<=subexp) look-behind
(?<!subexp) negative look-behind

With ruby 1.8, you can install Oniguruma.

Regards,
Thomas.

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      02-22-2008
J. Cooper wrote:
> i.e. is there any problem that can't be solved without lookbehind?


Yes, there are. For example: you want to match any occurence of "bar" except
if it is preceeded by "foo". I.e. you'd want to match "blabar" or "oofbar",
but not "foobar". You can't do that without negative lookbehind.
It might be interesting to note though, that any such problem could also not
be solved by a regular grammar, so "regular" expressions that need lookbehind
aren't, as such, regular anymore.

HTH,
Sebastian
--
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
ThoML
Guest
Posts: n/a
 
      02-22-2008
> Yes, there are. For example: you want to match any occurence of "bar" except
> if it is preceeded by "foo". I.e. you'd want to match "blabar" or "oofbar",
> but not "foobar".


I think it's important to state that the look-behind matches with zero
width, i.e. the match isn't included in the match.

If it's okay to include the prefix in the match (e.g., in a gsub, the
prefix could then be referenced as a group), this could also be
achieved without lookbehind:

require 'strscan'
# 0 1 2 3
# 0123456789012345678901234567890123
s = StringScanner.new('blabar oofbar foobar ofobar offbar')
# ^ ^ ^ ^
until s.eos?
m = s.scan_until(/([^o]|[^o]o|[^f]oo)(bar)/)
p s.pos
end

# =>
6
13
27
34

pos 20 is missing.

There are of course situations when this isn't possible.

Regards,
Thomas.

 
Reply With Quote
 
Mark Bush
Guest
Posts: n/a
 
      02-22-2008
ThoML wrote:
> There are of course situations when this isn't possible.


require 'strscan'
# 0 1 2 3
# 0123456789012345678901234567890123456789
s = StringScanner.new('obar blabar oofbar foobar ofobar offbar')
# ^ ^ ^ ^
until s.eos?
m = s.scan_until(/([^o]|[^o]o|[^f]oo)(bar)/)
p s.pos
end

# =>
11
18
32
39


However, with:
m = s.scan_until(/(([^o]|^)|([^o]|^)o|([^f]|^)oo)(bar)/)

=>
4
11
18
32
39


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

 
Reply With Quote
 
ThoML
Guest
Posts: n/a
 
      02-22-2008
> However, with:
> m = s.scan_until(/(([^o]|^)|([^o]|^)o|([^f]|^)oo)(bar)/)


Oh well. It's probably a good thing we have look-behind now.

Regards,
Thomas.
 
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
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments