473,385 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Find urls in plain text files

What is the best regular expression for finding urls in plain text
files?
(By urls I mean http://www.something.com, but also www.something.com,
or sa***@somewhere.com)

Salve
Nov 2 '07 #1
9 5082
Salve Håkedal wrote:
What is the best regular expression for finding urls in plain text
files?
(By urls I mean http://www.something.com, but also www.something.com,
or sa***@somewhere.com)

Salve
The simplest way is to use the one thing they all have in common. ".com".

strstr($text,'.com');
Nov 2 '07 #2
Salve Håkedal wrote:
What is the best regular expression for finding urls in plain text
files?
(By urls I mean http://www.something.com, but also www.something.com,
or sa***@somewhere.com)

Salve
I've used this before, but you're probably better off making your own
expression. Note that it's really loose and will get a lot of false positives -
especially file names - and it will cause havoc if you use it on HTML source. I
deliberately did not enter any Top-Level-Domain filtering, because there are so
many of them. You can replace the [a-z]{2,5} with something like (com|net|org)
if you don't need to worry about country codes.

The following expression should find strings that satisfy these conditions:

- optionally a http protocol identifier
- optionally a username(:password)@ string, which allows pretty much any
characters except for spaces and colons. This isn't RFC-standard, by the way.
- a hostname consisting of at least two and at most 34 labels, the last of which
has 2 to 5 alphabet letters (for weird new ones like aero and museum; you can
shorten it to 3 and still get the most common ones).
- optionally a path containing any characters apart from spaces, and /ending in
a non-punctuation character/. This last bit is vital because it avoids messing
up URLs at the end of a sentence.

(http:\/\/)?([^ :]+(:[^
]+)?@)?[a-z0-9]([a-z0-9i\-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?){0,32}\.[a-z]{2,5}(\/[^
]*[^" \.,;\)])?

(linebreaks are added by email client)

This is a case insensitive pattern, you'll need the i modifier.

--
Christoph Burschka
Nov 2 '07 #3
On 2007-11-02, Christoph Burschka <ch****************@rwth-aachen.dewrote:
Salve Håkedal wrote:
>What is the best regular expression for finding urls in plain text
files?
(By urls I mean http://www.something.com, but also www.something.com,
or sa***@somewhere.com)

Salve

I've used this before, but you're probably better off making your own
expression. Note that it's really loose and will get a lot of false positives -
especially file names - and it will cause havoc if you use it on HTML source. I
deliberately did not enter any Top-Level-Domain filtering, because there are so
many of them. You can replace the [a-z]{2,5} with something like (com|net|org)
if you don't need to worry about country codes.

The following expression should find strings that satisfy these conditions:

- optionally a http protocol identifier
- optionally a username(:password)@ string, which allows pretty much any
characters except for spaces and colons. This isn't RFC-standard, by the way.
- a hostname consisting of at least two and at most 34 labels, the last of which
has 2 to 5 alphabet letters (for weird new ones like aero and museum; you can
shorten it to 3 and still get the most common ones).
- optionally a path containing any characters apart from spaces, and /ending in
a non-punctuation character/. This last bit is vital because it avoids messing
up URLs at the end of a sentence.

(http:\/\/)?([^ :]+(:[^
]+)?@)?[a-z0-9]([a-z0-9i\-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?){0,32}\.[a-z]{2,5}(\/[^
]*[^" \.,;\)])?

(linebreaks are added by email client)

This is a case insensitive pattern, you'll need the i modifier.

--
Christoph Burschka
Thanks alot! I'll study this closely

--
Salve
Nov 2 '07 #4
..oO(Chris Gorospe)
>Salve Håkedal wrote:
>What is the best regular expression for finding urls in plain text
files?
(By urls I mean http://www.something.com, but also www.something.com,
or sa***@somewhere.com)
The simplest way is to use the one thing they all have in common. ".com".

strstr($text,'.com');
What about the other TLDs? There are _some_ more ...

Micha
Nov 2 '07 #5
"Salve Håkedal" <ik*************@slogedalen.nowrote in message
news:2-*********************@telenor.com...
On 2007-11-02, Christoph Burschka <ch****************@rwth-aachen.de>
wrote:
>The following expression should find strings that satisfy these
conditions:

- optionally a http protocol identifier
- optionally a username(:password)@ string, which allows pretty much any
characters except for spaces and colons. This isn't RFC-standard, by the
way.
- a hostname consisting of at least two and at most 34 labels, the last
of which
has 2 to 5 alphabet letters (for weird new ones like aero and museum; you
can
shorten it to 3 and still get the most common ones).
- optionally a path containing any characters apart from spaces, and
/ending in
a non-punctuation character/. This last bit is vital because it avoids
messing
up URLs at the end of a sentence.

(http:\/\/)?([^ :]+(:[^
]+)?@)?[a-z0-9]([a-z0-9i\-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9\-]{0,61}[a-z0-9])?){0,32}\.[a-z]{2,5}(\/[^
]*[^" \.,;\)])?

(linebreaks are added by email client)

This is a case insensitive pattern, you'll need the i modifier.

--
Christoph Burschka

Thanks alot! I'll study this closely
Does it make your eyes and ears bleed,the way it does mine?
Been doin this stuff since the 70's - but regex still makes me cry.

On that note, I am at the moment, writing a function that could sure benefit
from some regex.
I just want to see if a string starts with "http(s)://", "news:", "mailto:",
"ftp:".
That's a pretty simple regex, right?
Hoooowwww?

Nov 2 '07 #6
"Sanders Kaufman" <bu***@kaufman.netwrites:
Does it make your eyes and ears bleed,the way it does mine?
Been doin this stuff since the 70's - but regex still makes me cry.

On that note, I am at the moment, writing a function that could sure benefit
from some regex.
I just want to see if a string starts with "http(s)://", "news:", "mailto:",
"ftp:".
That's a pretty simple regex, right?
Hoooowwww?
This should get you started:

$pattern = '/^(http(s)?:\/\/|news:|mailto:|ftp:)/';

$tests = array('http://www.google.com', 'https://www.google.com',
'news:comp.lang', 'mailto:te**@nowhere.com',
' http://www.google.com',
'bad_http://www.google.com',
'mailtobad:fdsa', 'ftp://ftp.host.net',
'ftpbad:', 'badftp://');

foreach($tests as $v) {
print "'".$v."'" . ' ~ ' .
(preg_match($pattern, $v)
? 'matches'
: 'doesn\'t match')."\n";
}
Nov 2 '07 #7
On 2007-11-02, Carl <c.******@gmail.comwrote:
"Sanders Kaufman" <bu***@kaufman.netwrites:
>Does it make your eyes and ears bleed,the way it does mine?
Been doin this stuff since the 70's - but regex still makes me cry.

On that note, I am at the moment, writing a function that could sure benefit
from some regex.
I just want to see if a string starts with "http(s)://", "news:", "mailto:",
"ftp:".
That's a pretty simple regex, right?
Hoooowwww?

This should get you started:

$pattern = '/^(http(s)?:\/\/|news:|mailto:|ftp:)/';

$tests = array('http://www.google.com', 'https://www.google.com',
'news:comp.lang', 'mailto:te**@nowhere.com',
' http://www.google.com',
'bad_http://www.google.com',
'mailtobad:fdsa', 'ftp://ftp.host.net',
'ftpbad:', 'badftp://');

foreach($tests as $v) {
print "'".$v."'" . ' ~ ' .
(preg_match($pattern, $v)
? 'matches'
: 'doesn\'t match')."\n";
}
Thank you, Carl, for the script.

But the regexp there is as simple at I could have written myself. What
I need is something that can find urls in a text file, and convert them
to links. And by urls I mean, as I wrote in OT: http://something.org as
well as for example www.someother.anytopdm and the url in the original
text may be in parantheses or for example at the end of a sentence, so
it'll have a . in the end. So on..

Christoph Burschka's is still the most promising, but I haven't had time
to understand and to try it out yet.

Salve
Nov 3 '07 #8
Salve Håkedal <ik*************@slogedalen.nowrites:
On 2007-11-02, Carl <c.******@gmail.comwrote:
>"Sanders Kaufman" <bu***@kaufman.netwrites:
>>Does it make your eyes and ears bleed,the way it does mine?
Been doin this stuff since the 70's - but regex still makes me cry.

On that note, I am at the moment, writing a function that could sure benefit
from some regex.
I just want to see if a string starts with "http(s)://", "news:", "mailto:",
"ftp:".
That's a pretty simple regex, right?
Hoooowwww?

This should get you started:

$pattern = '/^(http(s)?:\/\/|news:|mailto:|ftp:)/';

$tests = array('http://www.google.com', 'https://www.google.com',
'news:comp.lang', 'mailto:te**@nowhere.com',
' http://www.google.com',
'bad_http://www.google.com',
'mailtobad:fdsa', 'ftp://ftp.host.net',
'ftpbad:', 'badftp://');

foreach($tests as $v) {
print "'".$v."'" . ' ~ ' .
(preg_match($pattern, $v)
? 'matches'
: 'doesn\'t match')."\n";
}

Thank you, Carl, for the script.

But the regexp there is as simple at I could have written myself. What
I need ...
--8<-- message cut -->8--
Salve,

My response was a followup to Sanders, not your O.P., I assumed that
the previous posts answered you question already.

The question you posted is quite common, and google'n should turn
up enough examples that you should'nt have to do much to get it working
decently enough.

For starters, this looks promising, though I haven't tested it (from
the 1st results page of a google search)

http://immike.net/blog/2007/04/06/5-...r-should-know/

--
Hope that helps,
Carl.
Nov 3 '07 #9
Salve Håkedal:
What is the best regular expression for finding urls in plain text
files?

Matching URLs of every scheme with a single regular expression would
be
incredibly complex and complicated. For example, matching mailto URLs
would entail the notorious regular expression for matching e-mail
addresses. Even matching URLs of individual schemes would require
careful study of both RFC3986 and the specification that governs that
particular URL scheme. Moreover, if you want to turn partial URLs
such as www.example.com into complete URLs, you would need to define
your own heuristics for doing so.

Upshot is, you're in a world of hurt.

--
Jock

Nov 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
4
by: hoke | last post by:
I want to display plain text files in the browser. The files contain html and javascript and have a .txt extension. This works fine with files with just html. Unfortunately when showing files with...
14
by: Akseli Mäki | last post by:
Hi, Hopefully this is not too much offtopic. I'm working on a FAQ. I want to make two versions of it, plain text and HTML. I'm looking for a tool that will make a plain text doc out of the...
7
by: AES | last post by:
Encountered a URL containing a comma the other day -- the first time I've ever noticed that, so far as I can recall. It worked fine, however, and I gather commas are legal in URLs. Out of...
10
by: Eric Lindsay | last post by:
This may be too far off topic, however I was looking at this page http://www.hixie.ch/advocacy/xhtml about XHTML problems by Ian Hickson. It is served as text/plain, according to Firefox...
19
by: Blair P. Houghton | last post by:
I'm just learning Python, so bear with. I was messing around with the webbrowser module and decided it was pretty cool to have the browser open a URL from within a python script, so I wrote a...
0
by: Shat T. Cat | last post by:
Hello, I have a program that I originally wrote in VB6 that breaks down plain-text Profit & Loss reports from my organization's Accounting system into separate files for each Cost Center (office...
3
by: AlecL | last post by:
Hi All, I am trying to capture the value of a textbox as a result of a button click event in a repeater, but it can't find the textbox. Here is what I am trying to do in the code for the click...
8
by: Bruno Rafael Moreira de Barros | last post by:
I have this framework I'm building in PHP, and it has Search Engine Friendly URLs, with site.com/controller/page/args... And on my View files, I have <?=$this->baseURL;?to print the base URL on the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.