Forum: Ruby Hopefully simple regex

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C8840bff779d9acaa15d1628f7869d12?d=identicon&s=25 stephen O'D (Guest)
on 2008-12-17 23:20
(Received via mailing list)
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.
C8840bff779d9acaa15d1628f7869d12?d=identicon&s=25 stephen O'D (Guest)
on 2008-12-17 23:33
(Received via mailing list)
On Dec 17, 10:15 pm, "stephen O'D" <stephen.odonn...@gmail.com> 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?
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-17 23:35
(Received via mailing list)
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
C8840bff779d9acaa15d1628f7869d12?d=identicon&s=25 stephen O'D (Guest)
on 2008-12-17 23:45
(Received via mailing list)
> Coming in 2009: 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!
18d3c84ca5a017fe3e96490afaea28aa?d=identicon&s=25 Richard Conroy (Guest)
on 2008-12-18 13:09
(Received via mailing list)
On Wed, Dec 17, 2008 at 10:37 PM, stephen O'D
<stephen.odonnell@gmail.com> wrote:
> I have learned something from the last hour of trying!
>

http://www.rubular.com is an awesome way of composing and testing ruby
regular expressions. Highly recommended.
This topic is locked and can not be replied to.