Find IP with a RegExp

M

Mark A. Odell

(e-mail address removed) (Vinicius) wrote in

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

Received: from unknown (HELO u7b3u5) ([email protected] with login)

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

Alexander Bartolich

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
 
J

Jeremy Yallop

Alexander said:
begin followup to Mark A. Odell:

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.
 
S

Seth Morecraft

Alexander said:
begin followup to Mark A. Odell:



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 ~
 
A

Alexander Bartolich

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
 
J

Jeremy Yallop

Alexander said:
begin followup to Jeremy Yallop:

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.
 
A

Alexander Bartolich

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.
 
J

Jeremy Yallop

Alexander said:
begin followup to Jeremy Yallop:

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.
 
K

Keith Thompson

Alexander Bartolich said:
begin followup to Jeremy Yallop:

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.
 
A

Alexander Bartolich

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 <[email protected]>
 
D

Derk Gwen

(e-mail address removed) (Vinicius) wrote:
# Hi people,
#
# How can I find a IP adress in a string using regexp?
#
# Received: from unknown (HELO u7b3u5) ([email protected] 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) ([email protected] with login)}
if {[regexp $d$p$d$p$d$p$d $string ip]} {
puts $ip
}
:eof
 
J

Jeremy Yallop

Alexander said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top