Help for a little regexp

Hi everyone,

So I am new to ruby, and I need help for a regexp.
I want to stock the letter and the number in this sting "e = 11.1607"

I tried with this regexp:

/(?<letter>^\w+)\/(?<frequency>\d{1,2}.\d+)/

But it does not match anything... But I tried separatly and it returns
me the good things. I do not understand what's the problem ^^'

Thank you to everyone who gonna help me ! :slight_smile:

路路路

--
Posted via http://www.ruby-forum.com/.

Hi,

Quentin RG wrote in post #1066270:

/(?<letter>^\w+)\/(?<frequency>\d{1,2}.\d+)/

But it does not match anything... But I tried separatly and it returns
me the good things. I do not understand what's the problem ^^'

I don't know how you've come to the "\/" part of the regex. This makes
the regex match only strings with some word characters followed by a
slash followed by some digits. For example the string "e/12.1516".

I think you rather want "\s*=\s*" instead of the "\/" (an equals sign
between spaces).

By the way: The dot "." has to be escaped. Otherwise it will match *any*
character except newlines. And I think you should replace the "\w" with
alphabetic characters: "[A-Za-z]". Because the \w also matches digits
and the underscore.

路路路

--
Posted via http://www.ruby-forum.com/\.

This works for me. Note that the start anchor "^" is outside of the
group, and that between both groups you need to match spaces and "=".
Also the "." has to be escaped to be matched exactly, otherwise it's
any character:

1.9.2p290 :026 > m = string.match
/^(?<letter>\w+)\s*=\s*(?<frequency>\d{1,2}\.\d+)/
=> #<MatchData "e = 11.1607" letter:"e" frequency:"11.1607">
1.9.2p290 :030 > m.names
=> ["letter", "frequency"]
1.9.2p290 :031 > m["letter"]
=> "e"
1.9.2p290 :032 > m["frequency"]
=> "11.1607"

Jesus.

路路路

On Wed, Jun 27, 2012 at 9:24 AM, Quentin R-G <lists@ruby-forum.com> wrote:

Hi everyone,

So I am new to ruby, and I need help for a regexp.
I want to stock the letter and the number in this sting "e = 11.1607"

I tried with this regexp:

/(?<letter>^\w+)\/(?<frequency>\d{1,2}.\d+)/

But it does not match anything... But I tried separatly and it returns
me the good things. I do not understand what's the problem ^^'

Thank you to everyone who gonna help me ! :slight_smile:

Okay, thank you guys for your help and advices :slight_smile:
It works now!

路路路

--
Posted via http://www.ruby-forum.com/.

Even better, use [[:alpha:]]+ so that the expression also matches accented and foreign alphabetic characters.

  "d茅j脿 vu".scan(/\w+/)
  => ["d", "j", "vu"]

  "d茅j脿 vu".scan(/[[:alpha:]]+/)
  => ["d茅j脿", "vu"]

路路路

On 06/27/2012 09:34 AM, Jan E. wrote:

Quentin RG wrote in post #1066270:

/(?<letter>^\w+)\/(?<frequency>\d{1,2}.\d+)/

And I think you should replace the "\w" with alphabetic characters:
"[A-Za-z]". Because the \w also matches digits

--
Lars Haugseth

In the future you should check out rubular.com. It's always my first stop
when I'm working with regexes.

路路路

On Jun 27, 2012 3:58 AM, "Quentin RG" <lists@ruby-forum.com> wrote:

Okay, thank you guys for your help and advices :slight_smile:
It works now!

--
Posted via http://www.ruby-forum.com/\.