Hy,
I would like to know how to extract a string delimited by too =20
identical
characters from an another string(of any length).
ex string=3D"rzerze@foo@rezrzgrtez" how to get (or match) the foo =20
string
irb(main):016:0> string=3D"rzerze@foo@rezrzgrtez"
=3D> "rzerze@foo@rezrzgrtez"
irb(main):017:0> (string.match /@(.*?)@/)[1]
=3D> "foo"
I don't quite understand the rest of your requirement. You mean =20
delimited
by a string, i.e. the @ above is a variable, or by two of any =20
character present
in a string? If it's the former:
irb(main):018:0> delimiter =3D "@"
=3D> "@"
irb(main):019:0> (string.match /#{delimiter}(.*?)#{delimiter}/)[1]
=3D> "foo"
if it's the latter, something like this might help:
irb(main):024:0> re =3D Regexp.new("([#{delimiter}])(.*)?\\1")
=3D> /([@abcde])(.*)?\1/
irb(main):025:0> string.match(re)[2]
=3D> "rze@foo@rezrzgrt"
It found the first 'e' as the delimiter, don't know why it took the
last 'e' as the other part of the delimiter, since I used a non-greedy
group for the middle part. Any ideas, someone?
Hope this helps,
Jesus.