From: Brian Candler Date: 2012-10-04T15:18:49+09:00 Subject: Re: sparse xml string 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="kl122ik3o" ? >>> >> 1.9.3p125 :002 > str.scan /[[:alnum:]]+<\/a>/ >> => ["kl1", "22ik", "3o"] >> >> 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 .* 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="kl122ik3o" => "kl122ik3o" >> str.scan /.*<\/a>/ => ["kl122ik3o"] That is: the opening tag is , the content is kl122ik3o, and the closing tag is . 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>/ => ["kl1", "22ik", "3o"] 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/.