Match every word except a substring

Hi,

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

@Regexp = ‘.*(){0}’
text = ‘…a simple string’
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.

On Nov 27, 9:13 am, T5in9tao Tsingtao [email protected] wrote:

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 ‘’ which is
equivalent to the entire contents of text (‘… a simple string’
is matched by '.
’ because that is followed by zero occurrences of ‘</
end>’).

Since you grouped the ‘’, the scan captures zero occurrences of
that (like you asked it to), which is nil in Ruby.

Try this:

puts ‘…a simple string’.scan(/(.*)</end>/)
…a simple string
=> nil

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

puts ‘…first matchsecond’.scan(/(.*?)</end>/)
…first match
second
=> nil

This matches any characters until the first occurrence of ‘’.
Without the extra ‘?’, ‘.*’ would match everything up to the final
occurrence of ‘’. Try it and see.

Jeremy

Sugoi :wink:

Thanks for your help, Jeremy.