[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Match every word except a substring

T5in9tao Tsingtao

11/27/2007 3:13:00 PM

Hi,

I try to write a regexp which match all possible chars, except a
substring (like </end> tag in my case).

@Regexp = '.*(</end>){0}'
text = '...a simple string</end>'
puts text.scan(/#{@Regexp}/).to_s

Normaly, that could be okay because of {0} but Ruby return nothing. Do
you have a idea about?

Thanks.
--
Posted via http://www.ruby-....

2 Answers

yermej

11/27/2007 4:48:00 PM

0

On Nov 27, 9:13 am, T5in9tao Tsingtao <t5in9...@gmail.com> wrote:
> Hi,
>
> I try to write a regexp which match all possible chars, except a
> substring (like </end> tag in my case).
>
> @Regexp = '.*(</end>){0}'
> text = '...a simple string</end>'
> puts text.scan(/#{@Regexp}/).to_s
>
> Normaly, that could be okay because of {0} but Ruby return nothing. Do
> you have a idea about?
>
> Thanks.
> --
> Posted viahttp://www.ruby-....

Using {0} doesn't make sense to me...it seems that you're looking for
any characters '.*' followed by zero occurrences of '</end>' which is
equivalent to the entire contents of text ('... a simple string</end>'
is matched by '.*' because that is followed by zero occurrences of '</
end>').

Since you grouped the '</end>', the scan captures zero occurrences of
that (like you asked it to), which is nil in Ruby.

Try this:
> puts '...a simple string</end>'.scan(/(.*)<\/end>/)
....a simple string
=> nil

That captures any and all characters until it reaches '</end>', which
I think is what you want. However, if you are parsing a file that may
have multiple occurrences of '</end>', you'll want to use the non-
greedy version of the above:

> puts '...first match</end>second</end>'.scan(/(.*?)<\/end>/)
....first match
second
=> nil

This matches any characters until the first occurrence of '</end>'.
Without the extra '?', '.*' would match everything up to the final
occurrence of '</end>'. Try it and see.

Jeremy

T5in9tao Tsingtao

11/27/2007 5:19:00 PM

0

Sugoi ;)

Thanks for your help, Jeremy.
--
Posted via http://www.ruby-....