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

Réponse
 
LinkBack Outils de la discussion
Vieux 18/06/2008, 21h05   #1
Petr Dupovnik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut regular expression

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

Hello

Suppose I have a string with some repeating patterns:

string = "some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]"

I want catch all instances of "[.*]" in this line - without the square
brackets. - in the above example that would be 'sdfsdf.wer', 'vbnfg.thy',
and 'jkhjkhjk.345'.

What regular expression would pull each instance of "[.*]" into a separate
element in an array?

my_match=string.match('\[(\w+\.\w{3}\]')

This only catches the first match, and ignores the second and third.

grateful for any .

Petr.

  Réponse avec citation
Vieux 18/06/2008, 21h15   #2
Sebastian Hungerecker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regular expression

Petr Dupovnik wrote:
> What regular expression would pull each instance of "[.*]" into a separate
> element in an array?


"some miscellaneous text [sdfsdf.wer], some more miscellaneous text
[vbnfg.thy], and yet more text [jkhjkhjk.345]".scan(/\[[^\]]+\]/).flatten
=> ["[sdfsdf.wer]", "[vbnfg.thy]", "[jkhjkhjk.345]"]

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

  Réponse avec citation
Vieux 18/06/2008, 21h17   #3
Kyle Schmitt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regular expression

On Wed, Jun 18, 2008 at 3:05 PM, Petr Dupovnik <petrdupovnik@gmail.com> wrote:
> Hello
>
> Suppose I have a string with some repeating patterns:
>
> string = "some miscellaneous text [sdfsdf.wer], some more miscellaneous text
> [vbnfg.thy], and yet more text [jkhjkhjk.345]"
>
> I want catch all instances of "[.*]" in this line - without the square
> brackets. - in the above example that would be 'sdfsdf.wer', 'vbnfg.thy',
> and 'jkhjkhjk.345'.

string.scan /(\[[^\]]*\])/
would do the trick
..although you could also use split with that same regex if you needed
the rest of the data for something.

scan is only keeping the saved part of the regex, as marked by our parenthesies
Inside of them is the slightly ugly statement
\[[^\]*]\]
since brackets are special in regexs, we have to escape them first,
hence the \[ and \] stuff
So it matches one bracket, [, and anything that isn't another bracket,
], followed by one bracket, ].

Does that make sense?

--Kyle

  Réponse avec citation
Vieux 18/06/2008, 21h26   #4
Petr Dupovnik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regular expression

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

Perfect.

Thanks Kyle & Sebastian.

On Wed, Jun 18, 2008 at 4:17 PM, Kyle Schmitt <kyleaschmitt@gmail.com>
wrote:

> On Wed, Jun 18, 2008 at 3:05 PM, Petr Dupovnik <petrdupovnik@gmail.com>
> wrote:
> > Hello
> >
> > Suppose I have a string with some repeating patterns:
> >
> > string = "some miscellaneous text [sdfsdf.wer], some more miscellaneous

> text
> > [vbnfg.thy], and yet more text [jkhjkhjk.345]"
> >
> > I want catch all instances of "[.*]" in this line - without the square
> > brackets. - in the above example that would be 'sdfsdf.wer', 'vbnfg.thy',
> > and 'jkhjkhjk.345'.

> string.scan /(\[[^\]]*\])/
> would do the trick
> ..although you could also use split with that same regex if you needed
> the rest of the data for something.
>
> scan is only keeping the saved part of the regex, as marked by our
> parenthesies
> Inside of them is the slightly ugly statement
> \[[^\]*]\]
> since brackets are special in regexs, we have to escape them first,
> hence the \[ and \] stuff
> So it matches one bracket, [, and anything that isn't another bracket,
> ], followed by one bracket, ].
>
> Does that make sense?
>
> --Kyle
>
>


  Réponse avec citation
Vieux 18/06/2008, 22h09   #5
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regular expression

On Wed, Jun 18, 2008 at 10:15 PM, Sebastian Hungerecker
<sepp2k@googlemail.com> wrote:
> Petr Dupovnik wrote:
>> What regular expression would pull each instance of "[.*]" into a separate
>> element in an array?

>
> "some miscellaneous text [sdfsdf.wer], some more miscellaneous text
> [vbnfg.thy], and yet more text [jkhjkhjk.345]".scan(/\[[^\]]+\]/).flatten
> => ["[sdfsdf.wer]", "[vbnfg.thy]", "[jkhjkhjk.345]"]
>
> HTH,
> Sebastian
> --
> Jabber: sepp2k@jabber.org
> ICQ: 205544826
>
>



scan( /\[(.*?)\]/ ).flatten

is a shorter alternative showing the use of the non greedy Kleene Star "*?"
But often it is indeed a good idea to be very explicit in your regular
expressions.

Cheers
Robert

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

---
As simple as possible, but not simpler.
Albert Einstein

  Réponse avec citation
Vieux 19/06/2008, 14h15   #6
Kyle Schmitt
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: regular expression

On Wed, Jun 18, 2008 at 4:09 PM, Robert Dober <robert.dober@gmail.com> wrote:
> But often it is indeed a good idea to be very explicit in your regular
> expressions.


Often it's a good idea just because it keeps you from using explicit
language when debugging your code later....

  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 00h19.


É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,13002 seconds with 14 queries