PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Regexp Capture Access
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Regexp Capture Access

Réponse
 
LinkBack Outils de la discussion
Vieux 02/04/2008, 14h20   #1
Oliver Saunders
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regexp Capture Access

Given that:

%r{(\w,)+}.match('a,b,c')[0] #=> "a,b,"

and

%r{(\w,)+}.match('a,b,c')[1] #=> "b,"

How do I access the capture that contains "a,"?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 02/04/2008, 15h04   #2
Mateusz Tybura
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

[Note: parts of this message were removed to make it a legal post.]

Try:

%r{(\w,)+}.match('a,b,c').to_s[0..1] #=> "a,"

2008/4/2, Oliver Saunders <oliver.saunders@gmail.com>:
>
> Given that:
>
> %r{(\w,)+}.match('a,b,c')[0] #=> "a,b,"
>
> and
>
> %r{(\w,)+}.match('a,b,c')[1] #=> "b,"
>
> How do I access the capture that contains "a,"?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>



--
My own blog (in polish) :
wujciol.yoyo.pl

  Réponse avec citation
Vieux 02/04/2008, 15h27   #3
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

On Wed, Apr 2, 2008 at 3:20 PM, Oliver Saunders
<oliver.saunders@gmail.com> wrote:
> Given that:
>
> %r{(\w,)+}.match('a,b,c')[0] #=> "a,b,"
>
> and
>
> %r{(\w,)+}.match('a,b,c')[1] #=> "b,"
>
> How do I access the capture that contains "a,"?
> --
> Posted via http://www.ruby-forum.com/.
>
>

Use scan

"a,b,c".scan(/\w+,/) --> ['a,', 'b,']
and in order to have the whole match you have to join the result of scan again.

HTH
Robert

--
http://ruby-smalltalk.blogspot.com/

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

  Réponse avec citation
Vieux 02/04/2008, 15h31   #4
mutahhir hayat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

[Note: parts of this message were removed to make it a legal post.]

The reason why it's acting like this is because Regexes try to find the
largest possible match from your expression. Hence, your expression

%r{(\w,)+}.match('a,b,c')[0]

will return the first match (and largest) "a,b,"


Im sure you're asking this for a bigger reason, so you have to refine your
regular expression for that specific purpose. if you just want to split
based on a "," then simply do 'a,b,c'.split(',') which returns an array
["a", "b", "c"]

If any other, please post the actual problem.

HTH
Mutahhir.


On Wed, Apr 2, 2008 at 10:04 PM, Mateusz Tybura <wujciol@gmail.com> wrote:

> Try:
>
> %r{(\w,)+}.match('a,b,c').to_s[0..1] #=> "a,"
>
> 2008/4/2, Oliver Saunders <oliver.saunders@gmail.com>:
> >
> > Given that:
> >
> > %r{(\w,)+}.match('a,b,c')[0] #=> "a,b,"
> >
> > and
> >
> > %r{(\w,)+}.match('a,b,c')[1] #=> "b,"
> >
> > How do I access the capture that contains "a,"?
> >
> > --
> > Posted via http://www.ruby-forum.com/.
> >
> >

>
>
> --
> My own blog (in polish) :
> wujciol.yoyo.pl
>


  Réponse avec citation
Vieux 02/04/2008, 15h31   #5
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

On Wed, Apr 2, 2008 at 8:20 AM, Oliver Saunders
<oliver.saunders@gmail.com> wrote:
> Given that:
>
> %r{(\w,)+}.match('a,b,c')[0] #=> "a,b,"
>
> and
>
> %r{(\w,)+}.match('a,b,c')[1] #=> "b,"
>
> How do I access the capture that contains "a,"?


Maybe leave the plus symbol (+) out?

r = /(\w,)/
r.match('hi,a,b,c')[1]
=> "i,"

I'd probably use #scan, or even #split, instead.

Todd

  Réponse avec citation
Vieux 02/04/2008, 15h39   #6
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

On Wed, Apr 2, 2008 at 9:27 AM, Robert Dober <robert.dober@gmail.com> wrote:
> Use scan
>
> "a,b,c".scan(/\w+,/) --> ['a,', 'b,']
> and in order to have the whole match you have to join the result of scan again.
>
> HTH
> Robert


Yeah, I was thinking of that regexp also, but I assumed the OP wanted
the last word letter before a comma.

Oliver, what is it that you're looking for?

Todd

  Réponse avec citation
Vieux 02/04/2008, 16h28   #7
Oliver Saunders
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

Nevermind. I was trying to parse CSV. I thought pre-written tools
weren't capable of understanding quoted values with commas inside but I
was wrong so I can use them.

I do find it amazing that you can't access multiple matches for a single
sub-pattern though. That's a serious limitation IMO.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 02/04/2008, 16h45   #8
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regexp Capture Access

On Wed, Apr 2, 2008 at 10:28 AM, Oliver Saunders
<oliver.saunders@gmail.com> wrote:
> Nevermind. I was trying to parse CSV. I thought pre-written tools
> weren't capable of understanding quoted values with commas inside but I
> was wrong so I can use them.
>
> I do find it amazing that you can't access multiple matches for a single
> sub-pattern though. That's a serious limitation IMO.


I'm not sure I understand this correctly, because I think your pattern
was wrong.

Todd

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h09.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14452 seconds with 16 queries