I’m new and I’m searching for an smart way to extract
the string ‘daniel’ from this line
--
I can find this line via: line =~ /<file( +)name( *)=( *“(.+)”)( *)>/
But how to extrace this sting?
You can generate a MatchData object, and then address that object
with numerical indices. Index 0 will be the whole match; index 1
will be the first () submatch, etc.
str = ‘’
m = /<file +name *= *“(.+)” *>/.match(str)
puts “Whole match is: #{m[0]}”
puts “First submatch is: #{m[1]}”
(You can also use the special variable $1 to get at the first
submatch.)
C:>irb
irb(main):001:0> line = ‘’
“<file name="daniel">”
irb(main):002:0> line =~ /<file +name *= *“(.+)” *>/
0
irb(main):003:0> puts $1
daniel
nil
irb(main):004:0> exit
C:>ruby -v
ruby 1.7.3 (2002-11-17) [i386-mswin32]
I took the liberty of removing the unwanted parenthesis from your regex.
I am assuming that you are new to Ruby and not to regex handling.
If not, let me know and will try and elaborate more …
HTH,
– shanko
“daniel” daniel.daniel@aon.at wrote in message
news:3e20bc01$0$37624$91cee783@newsreader01.highway.telekom.at…
hello,
I’m new and I’m searching for an smart way to extract
the string ‘daniel’ from this line
--
I can find this line via: line =~ /<file( +)name( *)=( *“(.+)”)( *)>/
But how to extrace this sting?
This question is only an example for much similar things
(I’m not searching for XML-Stuff).