Hi Florian,
I don't get what you mean by "too heavily rely", I mean, you are
always relying on the behaviour of the regexp library when it comes to
everything, so if you know how the dot, the * and the ? work in Ruby
why is your regexp better than the proposed one?
Anyway I have realized I had a mistake in my regexp (I had the ?
outside of the group, and not next to the *). So fixing that:
irb(main):034:0> string="rzerze@foo@rezrzgrtez"
=> "rzerze@foo@rezrzgrtez"
irb(main):035:0> re = Regexp.new("([#{delimiter}])(.*?)\\1")
=> /([abcde])(.*?)\1/
irb(main):036:0> string.match(re)[2]
=> "rz"
Now works as I intended. With your version:
irb(main):037:0> re = /([#{delimiter}])([^#{delimiter}]*)\1/
=> /([abcde])([^abcde]*)\1/
irb(main):038:0> string.match(re)[2]
=> "rz"
works too. So I would like to know why would be one preferred over the other.
Jesus.
Hi Jesus,
Because you could get problems when you have things like this:
"foo@fooo@bar@batz@foo"
you rely on the the behaviour of the dot. Meaning: yours matches
fooo@bar@batz
Thus, it ignores all inner delimiters.
In any case, mine matches:
[["fooo"],["batz"]] #String#scan output
Depending on how the dot is treated (greediness etc.), yours
could also have other interpretations (like mine) while mine is clearer in
that respect.
In a simple case with only 2 delimiters, it doesn't matter.
Actually, mine wasn't correct either, i also forgot the brackets...
/#{delimiter}([^#{delimiter}])*#{delimiter}/
For more readibility, here is the version with delimiter = @:
/@([^@]*)@/
Regards,
Florian Gilcher