10

I've got following text 'some-text-here' and try to get the 'text' word from it using groups.

If I use that expression /some-(\w+)-here/ all works fine, but if I try to apply grouping to it /some-(?<group_name>\w+)-here/ it's raise an error Undefined (?...) sequence.

What's I doing wrong?

(Ruby 1.9.2)

Upd: shame on me. It's all from my innatention. Yes, I've use RVM and my ruby version turned on 1.9.2. But I've tested that expression at http://rubular.com/ where it is written at the footer Rubular runs on Ruby 1.8.7. Ruby 1.8.7 and Ruby 1.9.2 have a different regular expression engine. So my expression works on 1.9.2, but does not on 1.8.7

1
  • 5
    It works on my Ruby 1.9.2 just fine. You're sure you're on this version? Commented Sep 1, 2011 at 20:56

2 Answers 2

10

For me that looks like you are looking at the wrong Ruby. Do you maybe have RVM installed?

1.9.2

>> RUBY_VERSION 
=> "1.9.2"
>> s='some-text-here' 
=> "some-text-here"
>> /some-(?<group_name>\w+)-here/ =~ s 
=> 0
>> group_name #=> "text"

1.8.7

>> RUBY_VERSION
=> "1.8.7"
>> s='some-text-here'
=> "some-text-here"
>> /some-(?<group_name>\w+)-here/ =~ s
SyntaxError: compile error
(irb):2: undefined (?...) sequence: /some-(?<group_name>\w+)-here/
    from (irb):2
Sign up to request clarification or add additional context in comments.

1 Comment

you got me thinking about the difference between versions. Thank you
0

These are my results with 1.9.2-p290:

irb(main):004:0> "some-text-here".match(/some-(?<test>\w+)-here/)
=> #<MatchData "some-text-here" test:"text">
irb(main):005:0> RUBY_VERSION
=> "1.9.2"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.