# Hopefully simple regex

**URL:** <https://www.ruby-forum.com/t/hopefully-simple-regex/155891>\
**Category:** Ruby\
**Created:** [December 17, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/hopefully-simple-regex/155891 "2008-12-17T23:20:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [December 17, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/hopefully-simple-regex/155891/1 "2008-12-17T23:20:32Z")

</div>

Hi,

I am trying to make a regex that will capture strings that look like  
‘@string’ surrounded by whitespace or at the start or end of a line -  
here is the spec:

require ‘test/unit’

class RegexTest \< Test::Unit::TestCase

def setup  
# this doesn’t work correctly for the fails scenarios  
@regex = /(@{1,1}\w+)/  
end

def test\_regex\_matches  
str = [  
“@bar some more text”,  
“@bar”,  
“sometext @bar text”,  
“sometext @bar”  
]  
str.each do |s|  
s =~ @regex  
assert\_equal(’@bar’, $1)  
end  
end

def test\_regex\_fails  
str = [  
“@@bar”,  
“sometext @@bar text”,  
“sometext@bar text”,  
“sometext@bar”  
]  
str.each do |s|  
s =~ @regex  
assert\_equal(nil, $1)  
end  
end

end

I have tried all sorts of combinations of character class and negated  
classes etc until my eyes have started glazing over - I really though  
this should be easy - can anyone suggest a regex that pass my spec  
above?

Thanks,

Stephen.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [December 17, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/hopefully-simple-regex/155891/2 "2008-12-17T23:33:25Z")

</div>

On Dec 17, 10:15 pm, “stephen O’D” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def setup  
> ]  
> “sometext@bar text”,  
> I have tried all sorts of combinations of character class and negated  
> classes etc until my eyes have started glazing over - I really though  
> this should be easy - can anyone suggest a regex that pass my spec  
> above?
> 
> Thanks,
> 
> Stephen.

It seems like this regex works:

/^(?:.\*\s+(@{1,1}\w+)|(@{1,1}\w+))/

Although you have to change the spec a little to check $1 and $2 for  
matches. Still seems a bit over complex to me - any better  
suggestions?

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [December 17, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/hopefully-simple-regex/155891/3 "2008-12-17T23:35:11Z")

</div>

Hi –

On Thu, 18 Dec 2008, stephen O’D wrote:

> def setup
> 
> # this doesn’t work correctly for the fails scenarios
> 
> @regex = /(@{1,1}\w+)/  
> end
> 
> I have tried all sorts of combinations of character class and negated  
> classes etc until my eyes have started glazing over - I really though  
> this should be easy - can anyone suggest a regex that pass my spec  
> above?

Try this:

@regex = /(?:^|\s)(@\w+)/

David

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [December 18, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/hopefully-simple-regex/155891/4 "2008-12-18T13:09:18Z")

</div>

On Wed, Dec 17, 2008 at 10:37 PM, stephen O’D  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have learned something from the last hour of trying!

[http://www.rubular.com](http://www.rubular.com) is an awesome way of composing and testing ruby  
regular expressions. Highly recommended.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [December 17, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/hopefully-simple-regex/155891/5 "2008-12-17T23:45:44Z")

</div>

> Coming in 2009: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))  
> Perfect - I didn’t know you could (or even think to try) putting the  
> ‘start of string’ anchor inside a group with an alternative! At least  
> I have learned something from the last hour of trying!
