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 > String extraction using RegExp
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
String extraction using RegExp

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 11h05   #1
Unmesh Gundecha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut String extraction using RegExp

Hi,

I want to extract "New User Registration" and "New User" from a string
as follows. I am using RegExp for this.

r1 = Regexp.new('\(\"([^\.]*)\"\)')
matches = r1.match('Page("New User Registration").Frame("New User")')

puts !matches.nil?
puts matches[0]
puts matches[1]
puts matches[2]

However above code returns only "New User Registration". Is there any
way to find the next match. Is something missing in this code?

Please advice.

Thanks in advance,
Unmesh
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/06/2008, 11h17   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String extraction using RegExp

On Monday 09 June 2008, Unmesh Gundecha wrote:
> Hi,
>
> I want to extract "New User Registration" and "New User" from a string
> as follows. I am using RegExp for this.
>
> r1 = Regexp.new('\(\"([^\.]*)\"\)')
> matches = r1.match('Page("New User Registration").Frame("New User")')
>
> puts !matches.nil?
> puts matches[0]
> puts matches[1]
> puts matches[2]
>
> However above code returns only "New User Registration". Is there any
> way to find the next match. Is something missing in this code?
>
> Please advice.
>
> Thanks in advance,
> Unmesh


Regexp.match and String.match only return the first match. If you want to get
all matches, you can use either String#scan or class StringScanner. In your
case, I think the better choice is the former:

str = 'Page("New User Registration").Frame("New User")'
str.scan(r1)
=> [["New User Registration"], ["New User"]]

String#scan scans the string for all matches and returns an array of arrays.
Each subarray contains all the groups for the corresponding match (if the
regexp doesn't contain groups, then String#scan returns an array of strings,
each of which is the matched text).

I hope this s

Stefano

  Réponse avec citation
Vieux 09/06/2008, 11h51   #3
Unmesh Gundecha
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: String extraction using RegExp

Stefano Crocco wrote:
> Regexp.match and String.match only return the first match. If you want
> to get
> all matches, you can use either String#scan or class StringScanner. In
> your
> case, I think the better choice is the former:
>
> str = 'Page("New User Registration").Frame("New User")'
> str.scan(r1)
> => [["New User Registration"], ["New User"]]
>
> String#scan scans the string for all matches and returns an array of
> arrays.
> Each subarray contains all the groups for the corresponding match (if
> the
> regexp doesn't contain groups, then String#scan returns an array of
> strings,
> each of which is the matched text).
>
> I hope this s
>
> Stefano


Thanks Stefano,

String#scan worked for me.

Cheers!!
Unmesh
--
Posted via http://www.ruby-forum.com/.

  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 03h44.


É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,09966 seconds with 11 queries