Find IP with a RegExp

  • Follow


Hi people,

How can I find a IP adress in a string using regexp?

Received: from unknown (HELO u7b3u5) (naxata@200.228.69.237 with login)


Thanks to all.
0
Reply vinicius (2) 1/9/2004 2:17:35 PM

vinicius@sordido.com.br (Vinicius) wrote in
news:6c8acc33.0401090617.6d618d53@posting.google.com: 

> How can I find a IP adress in a string using regexp?
> 
> Received: from unknown (HELO u7b3u5) (naxata@200.228.69.237 with login)

I'll answer this if you can tell me what this has to do with the C
language. 

-- 
- Mark ->
--
0
Reply nospam243 (468) 1/9/2004 2:19:37 PM


begin  followup to Mark A. Odell:
> I'll answer this if you can tell me what this has to do with
> the C language.

Apart from time travel C is not very useful. Use perl instead.

$ perl -ne 'm/\d+\.\d+\.\d+\.\d+/ && print $&' < data
200.228.69.237

-- 
F�r Google, Tux und GPL!
0
Reply alexander.bartolich (131) 1/9/2004 3:16:05 PM

Alexander Bartolich wrote:
> begin  followup to Mark A. Odell:
>> I'll answer this if you can tell me what this has to do with
>> the C language.
> 
> Apart from time travel C is not very useful. Use perl instead.
> 
> $ perl -ne 'm/\d+\.\d+\.\d+\.\d+/ && print $&' < data
> 200.228.69.237

Why do people keep posting broken perl "solutions" to comp.lang.c?

   $ perl -ne 'm/\d+\.\d+\.\d+\.\d+/ && print $&' <<< 12345.12345.12345.12345
   12345.12345.12345.12345

   "Some people, when confronted with a problem, think ``I know, I'll
    use regular expressions.'' Now they have two problems."    - JWZ

Jeremy.
0
Reply jeremy63 (294) 1/9/2004 3:50:05 PM

Alexander Bartolich wrote:
> begin  followup to Mark A. Odell:
> 
>>I'll answer this if you can tell me what this has to do with
>>the C language.
> 
> 
> Apart from time travel C is not very useful. Use perl instead.
> 
> $ perl -ne 'm/\d+\.\d+\.\d+\.\d+/ && print $&' < data
> 200.228.69.237
> 

Not useful? Funny that you'll still need C to compile the PERL 
processor, see perl.c in the PERL distribution :)

Seth

        ~ Let us linux ~


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 100,000 Newsgroups - 19 Different Servers! =-----
0
Reply chevystm (6) 1/9/2004 4:12:31 PM

begin  followup to Jeremy Yallop:
> Why do people keep posting broken perl "solutions" to comp.lang.c?

Because it's fun.

perl -ne 'm/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ && print $&' < data

>    "Some people, when confronted with a problem, think ``I know, I'll
>     use regular expressions.'' Now they have two problems."    - JWZ

-- 
# The Slam Dunk Answer
# They can go with me, or they can go with you, or they can go nowhere.
# But gee, the last two options are the same.
-- Larry Wall in <1994Jul28.193540.9498@netlabs.com>
0
Reply alexander.bartolich (131) 1/9/2004 7:56:30 PM

Alexander Bartolich wrote:
> begin  followup to Jeremy Yallop:
>> Why do people keep posting broken perl "solutions" to comp.lang.c?
> 
> Because it's fun.
> 
> perl -ne 'm/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ && print $&' < data

Still broken.  It wrongly accepts "333.444.555.666" (and doesn't
recognize "_127.0.0.1_", which perhaps it should).

Jeremy.
0
Reply jeremy63 (294) 1/9/2004 8:21:09 PM

begin  followup to Jeremy Yallop:
> Still broken.  It wrongly accepts "333.444.555.666"

Well, inet_aton has to be good for something. And whether the
overhead of a regular expression is better than than a brute
force loop with strstr ... is really just another kind of dick
sizw war.

> (and doesn't recognize "_127.0.0.1_", which perhaps it should).

By the same logic you can say it should also recognize IPv6.
But then who knows what the OP really wants.

-- 
F�r Google, Tux und GPL!
0
Reply alexander.bartolich (131) 1/9/2004 9:48:29 PM

Alexander Bartolich wrote:
> begin  followup to Jeremy Yallop:
>> Still broken.  It wrongly accepts "333.444.555.666"
> 
> Well, inet_aton has to be good for something. And whether the
> overhead of a regular expression is better than than a brute
> force loop with strstr ... is really just another kind of dick
> sizw war.

Correctness, not overhead, is the primary issue.  All the code in this
thread that uses regular expressions has been hopelessly broken.  This
isn't the right place to debug Perl code, though, as pointed out
already.

Jeremy.

0
Reply jeremy63 (294) 1/9/2004 10:21:21 PM

Alexander Bartolich <alexander.bartolich@gmx.at> writes:
> begin  followup to Jeremy Yallop:
> > Why do people keep posting broken perl "solutions" to comp.lang.c?
> 
> Because it's fun.
> 
> perl -ne 'm/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ && print $&' < data

Not really.  The above solution recognizes many things that look like
IP addresses, but are invalid.  It may also fail to recognize some
things that are valid IP addresses.  I won't go into the details,
because this (a) isn't the right place for it, and (b) I'm not sure of
the exact definition of a valid IP address (or, rather, of the textual
representation of a valid IP address).

The use of Perl is obviously off-topic, but a complete response would
require discussion of the RFC or RFCs that define the format, and
would probably veer off into IPV6 issues as well.  If I tried to
discuss it here, I would miss the opportunity to have my inevitable
mistakes corrected by experts.

C has no built-in support for regular expressions, though of course
numerous regular expression packages have been written in C.

If you want to present a definition of a valid IP address and ask how
to recognize strings that match the definition in portable C, this is
the place to discuss it -- ideally by posting a short working code
sample and asking why it doesn't work.  Otherwise, there are more
appropriate newsgroups.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
0
Reply kst-u (21549) 1/9/2004 10:54:13 PM

begin  followup to Jeremy Yallop:
> Correctness, not overhead, is the primary issue.

This is a meaningless sequence of buzzwords.
Correctness primarily requires a correct definition of correctness.

> All the code in this thread that uses regular expressions has
> been hopelessly broken.

No other code has been posted. Ok, this may be because the whole
issue is way off-topic. Aggravated by severe immunity against
trolling amongst regulars.  

Anyway, the funny thing about your definition of 'hopeless' is
how little change is required after each redefinition of the task
at hand.

#!/usr/bin/perl -wn
BEGIN { use Socket; }
m/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ && inet_aton($&) && print $&;

-- 
The Paternal Patter Answer
# That's okay, <name>, I understand where you're coming from.
# I once went through the rebellious stage you're going through,
# but eventually I settled down and started getting some real work
# done.  These things just take time.  Be patient with yourself.
-- Larry Wall <1994Jul28.193540.9498@netlabs.com>
0
Reply alexander.bartolich (131) 1/9/2004 10:59:04 PM

vinicius@sordido.com.br (Vinicius) wrote:
# Hi people,
# 
# How can I find a IP adress in a string using regexp?
# 
# Received: from unknown (HELO u7b3u5) (naxata@200.228.69.237 with login)

tclsh <<':eof'
set d {([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])}
set p {[.]}
set string {Received: from unknown (HELO u7b3u5) (naxata@200.228.69.237 with login)}
if {[regexp $d$p$d$p$d$p$d $string ip]} {
  puts $ip
}
:eof

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Title does not dictate behaviour.
0
Reply derkgwen (343) 1/9/2004 11:36:32 PM

Alexander Bartolich wrote:
> Anyway, the funny thing about your definition of 'hopeless' is
> how little change is required after each redefinition of the task
> at hand.

There has been no such redefinition.

> #!/usr/bin/perl -wn
[snip]

Sorry, but it's still both broken and off-topic.

Jeremy.
0
Reply jeremy63 (294) 1/10/2004 7:30:49 PM

12 Replies
79 Views

(page loaded in 0.145 seconds)

Similiar Articles:


















7/18/2012 5:12:49 AM


Reply: