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 29/03/2008, 07h45   #1
Tony De
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regular Expression

On to my next learning exercise. As I parse a file I need to pull an IP
address out a line. Now I thought a regular expression would be the
ticket, but it's giving me a problem. The follow line is an example
string I need to pull one of two IP address out of: (they are not
always formed the same)

Received: from mmds-111-19-22-30.twm.ca.internet.net (HELO
?192.168.1.2?) (222.222.222.22)

I need that last IP address. Now the problem is that I can't always
count on it being enclosed in paren's. Although I can expect the right
paren to always be there.

So here's my regex exp:
sourceip = line.scan(/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/)
And as you might expect, it is pulling both IP addresses. Is there a
way I can adjust the expression to grab the second IP testing for the
")" or is there another method I can use? Short of dissecting the
entire string backwards and testing whether I have a number or a char,
decimal and at most 3 chars from it, etc?

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

  Réponse avec citation
Vieux 29/03/2008, 08h05   #2
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular Expression

Hi --

On Sat, 29 Mar 2008, Tony De wrote:

> On to my next learning exercise. As I parse a file I need to pull an IP
> address out a line. Now I thought a regular expression would be the
> ticket, but it's giving me a problem. The follow line is an example
> string I need to pull one of two IP address out of: (they are not
> always formed the same)
>
> Received: from mmds-111-19-22-30.twm.ca.internet.net (HELO
> ?192.168.1.2?) (222.222.222.22)
>
> I need that last IP address. Now the problem is that I can't always
> count on it being enclosed in paren's. Although I can expect the right
> paren to always be there.
>
> So here's my regex exp:
> sourceip = line.scan(/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/)
> And as you might expect, it is pulling both IP addresses. Is there a
> way I can adjust the expression to grab the second IP testing for the
> ")" or is there another method I can use? Short of dissecting the
> entire string backwards and testing whether I have a number or a char,
> decimal and at most 3 chars from it, etc?


What you want is an IP address, possibly followed by ')' and
definitely coming at the end of the string (give or take a newline
character after it). That can be expressed like this:

/((\d{1,3}\.){3}\d{1,3})(?=\)?\Z)/

I've got 3 occurences of (\d{1,3}\.), followed by the same thing
without a dot. I've stipulated that this submatch be "looking at"
(i.e., positioned just before) an optional ')' followed by the end of
the string. (\Z gives you end of string, ignoring a possible terminal
newline.)

With your line, it gives you:

irb(main):039:0> line[re] # re.match(line)[0], or whatever
=> "222.222.222.22"


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!

  Réponse avec citation
Vieux 29/03/2008, 08h16   #3
Tony De
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular Expression

David A. Black wrote:
> Hi --
>
> On Sat, 29 Mar 2008, Tony De wrote:
>
>> count on it being enclosed in paren's. Although I can expect the right
>> paren to always be there.
>>
>> So here's my regex exp:
>> sourceip = line.scan(/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/)
>> And as you might expect, it is pulling both IP addresses. Is there a
>> way I can adjust the expression to grab the second IP testing for the
>> ")" or is there another method I can use? Short of dissecting the
>> entire string backwards and testing whether I have a number or a char,
>> decimal and at most 3 chars from it, etc?

>
> What you want is an IP address, possibly followed by ')' and
> definitely coming at the end of the string (give or take a newline
> character after it). That can be expressed like this:
>
> /((\d{1,3}\.){3}\d{1,3})(?=\)?\Z)/
>
> I've got 3 occurences of (\d{1,3}\.), followed by the same thing
> without a dot. I've stipulated that this submatch be "looking at"
> (i.e., positioned just before) an optional ')' followed by the end of
> the string. (\Z gives you end of string, ignoring a possible terminal
> newline.)
>
> With your line, it gives you:
>
> irb(main):039:0> line[re] # re.match(line)[0], or whatever
> => "222.222.222.22"
>
>
> David


David, you rock. I'll give it a try. Those expressions make my head
hurt. But I've been taking in
http://www.regular-expressions.info/tutorial.html. It seems to cover a
lot of foundation and application. Thanks again!

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

  Réponse avec citation
Vieux 29/03/2008, 13h21   #4
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular Expression

On Sat, Mar 29, 2008 at 8:16 AM, Tony De <tonydema@gmail.com> wrote:
> David A. Black wrote:
> > Hi --
> >
> > On Sat, 29 Mar 2008, Tony De wrote:
> >

>
> >> count on it being enclosed in paren's. Although I can expect the right
> >> paren to always be there.
> >>
> >> So here's my regex exp:
> >> sourceip = line.scan(/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/)
> >> And as you might expect, it is pulling both IP addresses. Is there a
> >> way I can adjust the expression to grab the second IP testing for the
> >> ")" or is there another method I can use? Short of dissecting the
> >> entire string backwards and testing whether I have a number or a char,
> >> decimal and at most 3 chars from it, etc?

> >
> > What you want is an IP address, possibly followed by ')' and
> > definitely coming at the end of the string (give or take a newline
> > character after it). That can be expressed like this:
> >
> > /((\d{1,3}\.){3}\d{1,3})(?=\)?\Z)/
> >
> > I've got 3 occurences of (\d{1,3}\.), followed by the same thing
> > without a dot. I've stipulated that this submatch be "looking at"
> > (i.e., positioned just before) an optional ')' followed by the end of
> > the string. (\Z gives you end of string, ignoring a possible terminal
> > newline.)
> >
> > With your line, it gives you:
> >
> > irb(main):039:0> line[re] # re.match(line)[0], or whatever
> > => "222.222.222.22"
> >
> >
> > David

>
> David, you rock. I'll give it a try. Those expressions make my head
> hurt. But I've been taking in
> http://www.regular-expressions.info/tutorial.html. It seems to cover a
> lot of foundation and application. Thanks again!


Another possibility (if I understood correctly): check for the
mandatory ')' in the HELO part, followed by any character (could be
changed by the specific spaces), followed by the numbers and dots for
the IP:

a = "Received: from mmds-111-19-22-30.twm.ca.internet.net (HELO
?192.168.1.2?) (222.222.222.22)"
a.match(/\).*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)[1]

gives: "222.222.222.22"

Jesus.

  Réponse avec citation
Vieux 30/03/2008, 01h10   #5
Tony De
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular Expression

Jesús Gabriel y Galán wrote:
> On Sat, Mar 29, 2008 at 8:16 AM, Tony De <tonydema@gmail.com> wrote:
>> >> sourceip = line.scan(/\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/)
>> > /((\d{1,3}\.){3}\d{1,3})(?=\)?\Z)/
>> > => "222.222.222.22"
>> >
>> >
>> > David

>>
>> David, you rock. I'll give it a try. Those expressions make my head
>> hurt. But I've been taking in
>> http://www.regular-expressions.info/tutorial.html. It seems to cover a
>> lot of foundation and application. Thanks again!

>
> Another possibility (if I understood correctly): check for the
> mandatory ')' in the HELO part, followed by any character (could be
> changed by the specific spaces), followed by the numbers and dots for
> the IP:
>
> a = "Received: from mmds-111-19-22-30.twm.ca.internet.net (HELO
> ?192.168.1.2?) (222.222.222.22)"
> a.match(/\).*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)[1]
>
> gives: "222.222.222.22"
>
> Jesus.



Thanks Jesus,

I appraciate your imput as well. You guys have been a great deal of
.

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

  Réponse avec citation
Vieux 31/03/2008, 18h39   #6
Andrew Stewart
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular Expression


On 29 Mar 2008, at 07:16, Tony De wrote:
> Those expressions make my head
> hurt. But I've been taking in
> http://www.regular-expressions.info/tutorial.html. It seems to
> cover a
> lot of foundation and application. Thanks again!


You may find Rubular ful for concocting your regular expressions
and decoding other people's.

http://www.rubular.com/

Regards,
Andy Stewart

-------
http://airbladesoftware.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 14h17.


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