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-forum.com/.

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