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 > Ruby regexp Match
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Ruby regexp Match

Réponse
 
LinkBack Outils de la discussion
Vieux 04/12/2007, 02h21   #1
John Sheahan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Ruby regexp Match

I am trying to convert some old Perl scripts to Ruby and am stuck at a
spot where my Perl script is doing a regular expression match on some
text pulled from a webpage.

In Perl, I was able to grab the text I wanted by putting () around the
regular expression which represented the data I wanted, then access it
with the x[0] method.

Is there something like this in Ruby? I was looking through the String
class and don't really see a method listed to do this.

I want to be able to do a kind of match like:

m/^Title([a-zA-Z0-9])//g

and access what's inside the () with x[0].

Thanks

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

  Réponse avec citation
Vieux 04/12/2007, 02h39   #2
botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby regexp Match

On 12/4/07, John Sheahan <contact@thirdorder.net> wrote:
> I want to be able to do a kind of match like:
> m/^Title([a-zA-Z0-9])//g
> and access what's inside the () with x[0].


ruby gives more (objects), see http://ruby-doc.org/core/classes/MatchData.html

some simple examples

t="Title123asdf"
#=> "Title123asdf"
/^Title([a-zA-Z0-9])/.match(t)
#=> #<MatchData:0x28b7b60>
/^Title([a-zA-Z0-9])/.match(t)[0]
#=> "Title1"
/^Title([a-zA-Z0-9])/.match(t)[1]
#=> "1"
$~
#=> #<MatchData:0x28afc94>
$~[0]
#=> "Title1"
$~[1]
#=> "1"
x=/^Title([a-zA-Z0-9])/.match(t)
#=> #<MatchData:0x28a8598>
x[0]
#=> "Title1"
x[1]

kind regards -botp

  Réponse avec citation
Vieux 04/12/2007, 02h40   #3
Justin Collins
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby regexp Match

John Sheahan wrote:
> I am trying to convert some old Perl scripts to Ruby and am stuck at a
> spot where my Perl script is doing a regular expression match on some
> text pulled from a webpage.
>
> In Perl, I was able to grab the text I wanted by putting () around the
> regular expression which represented the data I wanted, then access it
> with the x[0] method.
>
> Is there something like this in Ruby? I was looking through the String
> class and don't really see a method listed to do this.
>
> I want to be able to do a kind of match like:
>
> m/^Title([a-zA-Z0-9])//g
>
> and access what's inside the () with x[0].
>
> Thanks
>
> jackster
>


Yes.

irb(main):001:0> result = "Title1".match(/^Title([a-zA-Z0-9])/)
=> #<MatchData:0xb7d90654>
irb(main):002:0> result[1]
=> "1"
irb(main):003:0> $1
=> "1"
irb(main):004:0> Regexp.last_match[1]
=> "1"
irb(main):005:0> $~[1]
=> "1"


http://ruby-doc.org/core/classes/MatchData.html

-Justin


  Réponse avec citation
Vieux 04/12/2007, 02h54   #4
John Sheahan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby regexp Match

I got it!

thanks to everyone that posted a reply....you all were very ful!
jackster

Collins wrote:
> John Sheahan wrote:
>>
>> I want to be able to do a kind of match like:
>>
>> m/^Title([a-zA-Z0-9])//g
>>
>> and access what's inside the () with x[0].
>>
>> Thanks
>>
>> jackster
>>

>
> Yes.
>
> irb(main):001:0> result = "Title1".match(/^Title([a-zA-Z0-9])/)
> => #<MatchData:0xb7d90654>
> irb(main):002:0> result[1]
> => "1"
> irb(main):003:0> $1
> => "1"
> irb(main):004:0> Regexp.last_match[1]
> => "1"
> irb(main):005:0> $~[1]
> => "1"
>
>
> http://ruby-doc.org/core/classes/MatchData.html
>
> -Justin


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

  Réponse avec citation
Vieux 04/12/2007, 03h27   #5
MonkeeSage
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby regexp Match

On Dec 3, 8:54 pm, John Sheahan <cont...@thirdorder.net> wrote:
> I got it!
>
> thanks to everyone that posted a reply....you all were very ful!
> jackster
>
>
>
> Collins wrote:
> > John Sheahan wrote:

>
> >> I want to be able to do a kind of match like:

>
> >> m/^Title([a-zA-Z0-9])//g

>
> >> and access what's inside the () with x[0].

>
> >> Thanks

>
> >> jackster

>
> > Yes.

>
> > irb(main):001:0> result = "Title1".match(/^Title([a-zA-Z0-9])/)
> > => #<MatchData:0xb7d90654>
> > irb(main):002:0> result[1]
> > => "1"
> > irb(main):003:0> $1
> > => "1"
> > irb(main):004:0> Regexp.last_match[1]
> > => "1"
> > irb(main):005:0> $~[1]
> > => "1"

>
> >http://ruby-doc.org/core/classes/MatchData.html

>
> > -Justin

>
> --
> Posted viahttp://www.ruby-forum.com/.


You can also do like you would in perl...

"TitleA" =~ /^Title([a-zA-Z0-9])/
puts $1
# => A

Regards,
Jordan
  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 03h54.


É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,13029 seconds with 13 queries