[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expression for string.anotherstring

Bart Braem

9/15/2006 3:13:00 PM

I'm trying to validate a user mail address for a fixed domain with the rule
string =~ /\w+\.\w+/
this matches firstname.lastname, which is what I want.
But it also matches fistname.lastname@somedomain.com, which is not what I
want. Why does that match? I'm having this in Rails, but I guess the
quoting will not escape @ signs?

Bart
14 Answers

Paul Lutus

9/15/2006 3:29:00 PM

0

Bart Braem wrote:

> I'm trying to validate a user mail address for a fixed domain with the
> rule string =~ /\w+\.\w+/
> this matches firstname.lastname, which is what I want.
> But it also matches fistname.lastname@somedomain.com, which is not what I
> want. Why does that match? I'm having this in Rails, but I guess the
> quoting will not escape @ signs?

I think you want this: "string.string", but you don't want this:
"string.string@domain". Yes?

Have you tried:

string =~ /^\w+\.\w+$/

"^" means match at the beginning of the string, "$" means match at the end.
IOW the example must match the entire string. Is this what you wanted?

--
Paul Lutus
http://www.ara...

Rick DeNatale

9/15/2006 8:11:00 PM

0

On 9/15/06, Paul Lutus <nospam@nosite.zzz> wrote:
> Bart Braem wrote:
>
> > I'm trying to validate a user mail address for a fixed domain with the
> > rule string =~ /\w+\.\w+/
> > this matches firstname.lastname, which is what I want.
> > But it also matches fistname.lastname@somedomain.com, which is not what I
> > want.

> Have you tried:
>
> string =~ /^\w+\.\w+$/
>
> "^" means match at the beginning of the string, "$" means match at the end.
> IOW the example must match the entire string. Is this what you wanted?

Actually "$" matches at the end of the string or the first line break
('\n') whichever comes first.

\z literally matches at the end of the string, whereas
\Z matches either the end of the string unless the string ends with
'\n' in which case it matches just before that final '\n'

And if you want to match just before the first of an optional series
of trailing '\n's I think that this:

\n*\Z

works as the end of the RE.
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Harold Hausman

9/16/2006 4:04:00 AM

0

On 9/15/06, Bart Braem <bart.braem@gmail.com> wrote:
> I'm trying to validate a user mail address for a fixed domain with the rule
> string =~ /\w+\.\w+/
> this matches firstname.lastname, which is what I want.
> But it also matches fistname.lastname@somedomain.com, which is not what I
> want. Why does that match? I'm having this in Rails, but I guess the
> quoting will not escape @ signs?
>
> Bart
>
>

I'm no regex master but what about:
/^[^@]+/

One or more things thats not an '@' at the beginning of the string?

hth,
-Harold

Bart Braem

9/17/2006 5:44:00 PM

0

Paul Lutus wrote:

> I think you want this: "string.string", but you don't want this:
> "string.string@domain". Yes?
>
That's right.


> Have you tried:
>
> string =~ /^\w+\.\w+$/
>
> "^" means match at the beginning of the string, "$" means match at the
> end. IOW the example must match the entire string. Is this what you
> wanted?

That's a good suggestion, I'll give it a shot.
One question though: why does \w match with '@'? According to the
documentation \w == [a..zA..Z]??

Bart

MonkeeSage

9/17/2006 11:48:00 PM

0


Bart Braem wrote:
> One question though: why does \w match with '@'? According to the
> documentation \w == [a..zA..Z]??\w doesn't match @, it's just that your expression was too general:

test = lambda { |x| p $1 if x =~ /(\w+\.\w+)/ }
test.call('down@fraggle.roc') # => "fraggle.roc"
test.call('foo.bar@baz') # => "foo.bar"

I.e., you were matching on either side of the @

Regards,
Jordan

Bart Braem

9/18/2006 9:19:00 AM

0

MonkeeSage wrote:

> I.e., you were matching on either side of the @
>
Oh boy, you are right! Thanks for the explanation.
I will try it with:
string =~ /^\w+\.\w+\z/

But is there a ruby operator to look for matches on the entire string?

Thanks for your help
Bart

Paul Lutus

9/18/2006 9:42:00 AM

0

Bart Braem wrote:

> MonkeeSage wrote:
>
>> I.e., you were matching on either side of the @
>>
> Oh boy, you are right! Thanks for the explanation.
> I will try it with:
> string =~ /^\w+\.\w+\z/
>
> But is there a ruby operator to look for matches on the entire string?

string =~ /^\w+\.\w+$/

Must match the entire string. Because of multiline issues, where that is a
factor, you can always split on newlines in advance of this test to be sure
you are matching the entire string unambiguously.

--
Paul Lutus
http://www.ara...

Pit Capitain

9/18/2006 10:28:00 AM

0

Paul Lutus schrieb:
> Bart Braem wrote:
>>
>> I will try it with:
>> string =~ /^\w+\.\w+\z/
>>
>> But is there a ruby operator to look for matches on the entire string?
>
> string =~ /^\w+\.\w+$/
>
> Must match the entire string. Because of multiline issues, where that is a
> factor, you can always split on newlines in advance of this test to be sure
> you are matching the entire string unambiguously.

This would be unnecessarily complex. Just use Bart's regexp (with \z
instead of $). It matches the entire string.

Regards,
Pit

Alex LeDonne

9/18/2006 2:26:00 PM

0

On 9/17/06, Bart Braem <bart.braem@gmail.com> wrote:
> Paul Lutus wrote:
>
> > I think you want this: "string.string", but you don't want this:
> > "string.string@domain". Yes?
> >
> That's right.
>
>
> > Have you tried:
> >
> > string =~ /^\w+\.\w+$/
> >
> > "^" means match at the beginning of the string, "$" means match at the
> > end. IOW the example must match the entire string. Is this what you
> > wanted?
>
> That's a good suggestion, I'll give it a shot.
> One question though: why does \w match with '@'? According to the
> documentation \w == [a..zA..Z]??
>
> Bart

Bart-

Maybe I'm off base here, but are you trying to determine if a string
matches a pattern, or are you trying to capture a matching substring
from a string?

-Alex

dblack

9/18/2006 4:05:00 PM

0