# Match every word except a substring

**URL:** <https://www.ruby-forum.com/t/match-every-word-except-a-substring/121393>\
**Category:** Ruby\
**Created:** [November 27, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/match-every-word-except-a-substring/121393 "2007-11-27T16:13:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![t5in9tao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@t5in9tao](https://www.ruby-forum.com/u/t5in9tao)\
**Post date:** [November 27, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/match-every-word-except-a-substring/121393/1 "2007-11-27T16:13:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![t5in9tao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@t5in9tao](https://www.ruby-forum.com/u/t5in9tao)\
**Post date:** [November 27, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/match-every-word-except-a-substring/121393/2 "2007-11-27T17:54:29Z")

</div>

On Nov 27, 9:13 am, T5in9tao Tsingtao [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![t5in9tao](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@t5in9tao](https://www.ruby-forum.com/u/t5in9tao)\
**Post date:** [November 27, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/match-every-word-except-a-substring/121393/3 "2007-11-27T18:19:14Z")

</div>

Sugoi 😉

Thanks for your help, Jeremy.
