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:

/(?^\w+)/(?\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:

Hi,

Quentin RG wrote in post #1066270:

/(?^\w+)/(?\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.

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

On Wed, Jun 27, 2012 at 9:24 AM, Quentin R-G [email protected]
wrote:

me the good things. I do not understand what’s the problem ^^’

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

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
/^(?\w+)\s*=\s*(?\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.

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

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

Quentin RG wrote in post #1066270:

/(?^\w+)/(?\d{1,2}.\d+)/

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

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”]