ajay paswan wrote in post #1078256:
> Sam Duncan wrote in post #1078254:
>> On 10/02/2012 10:58 AM, ajay paswan wrote:
>>>>
>>>> Sam
>>> What if: str="<a>kl1</a><a>22ik</a><a>3o</a>" ?
>>>
>> 1.9.3p125 :002 > str.scan /<a>[[:alnum:]]+<\/a>/
>>   => ["<a>kl1</a>", "<a>22ik</a>", "<a>3o</a>"]
>>
>> Ping pong
>>
>> I think perhaps you should read up on regular expressions in Ruby =]
>>
>> Sam
>
> I have gone through it previously but could not figure it out, is '.'
> denotes any character?

Yes, and .* means zero or more times of any character, so you might 
think of <a>.*</a> to match an open tag, followed by any text, followed 
by a closing tag.

However this won't work the way you expect, because .* will match the 
largest amount of text it can while still matching the rest of the 
pattern.

>> str="<a>kl1</a><a>22ik</a><a>3o</a>"
=> "<a>kl1</a><a>22ik</a><a>3o</a>"
>> str.scan /<a>.*<\/a>/
=> ["<a>kl1</a><a>22ik</a><a>3o</a>"]

That is: the opening tag is <a>, the content is kl1</a><a>22ik</a><a>3o, 
and the closing tag is </a>. You probably hadn't thought of it like that 
:-)

You can fix this using .*?, which will consume the smallest amount of 
text it can while still matching the rest of the pattern.

>> str.scan /<a>.*?<\/a>/
=> ["<a>kl1</a>", "<a>22ik</a>", "<a>3o</a>"]

But as has been pointed out, regular expressions are not the right way 
to parse XML. Use a library specifically designed for XML parsing.

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