By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,147 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,147 IT Pros & Developers. It's quick & easy.

Find IP with a RegExp

P: n/a
Hi people,

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

Received: from unknown (HELO u7b3u5) (na****@200.228.69.237 with login)
Thanks to all.
Nov 14 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
vi******@sordido.com.br (Vinicius) wrote in
news:6c**************************@posting.google.c om:
How can I find a IP adress in a string using regexp?

Received: from unknown (HELO u7b3u5) (na****@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 ->
--
Nov 14 '05 #2

P: n/a
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!
Nov 14 '05 #3

P: n/a
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.
Nov 14 '05 #4

P: n/a
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! =-----
Nov 14 '05 #5

P: n/a
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 <19*******************@netlabs.com>
Nov 14 '05 #6

P: n/a
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.
Nov 14 '05 #7

P: n/a
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!
Nov 14 '05 #8

P: n/a
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.

Nov 14 '05 #9

P: n/a
Alexander Bartolich <al*****************@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) ks***@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"
Nov 14 '05 #10

P: n/a
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 <19*******************@netlabs.com>
Nov 14 '05 #11

P: n/a
vi******@sordido.com.br (Vinicius) wrote:
# Hi people,
#
# How can I find a IP adress in a string using regexp?
#
# Received: from unknown (HELO u7b3u5) (na****@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) (na****@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.
Nov 14 '05 #12

P: n/a
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.
Nov 14 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.