[Question]
  [
**Closed.** This question is [off-topic](/help/closed-questions). It is not currently accepting answers.
 
 
---
 Questions without an **objective primary winning criterion** are off-topic, as they make it impossible to indisputably decide which entry should win.
Closed 3 years ago.
**Locked**. This question and its answers are [locked](/help/locked-posts) because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
 
 
[Stroustrup](http://en.wikipedia.org/wiki/Bjarne_Stroustrup) has recently posted a series of posts debunking [popular myths about C++](http://isocpp.org/blog/2014/12/myths-3). The fifth myth is: “C++ is for large, complicated, programs only”. To debunk it, he wrote a simple C++ program **downloading a web page and extracting links from it**. Here it is:
```
#include <string>
#include <set>
#include <iostream>
#include <sstream>
#include <regex>
#include <boost/asio.hpp>
using namespace std;
set<string> get_strings(istream& is, regex pat)
{
    set<string> res;
    smatch m;
    for (string s; getline(is, s);)  // read a line
        if (regex_search(s, m, pat))
            res.insert(m[0]);              // save match in set
    return res;
}
void connect_to_file(iostream& s, const string& server, const string& file)
// open a connection to server and open an attach file to s
// skip headers
{
    if (!s)
        throw runtime_error{ "can't connect\n" };
    // Request to read the file from the server:
    s << "GET " << "http://" + server + "/" + file << " HTTP/1.0\r\n";
    s << "Host: " << server << "\r\n";
    s << "Accept: */*\r\n";
    s << "Connection: close\r\n\r\n";
    // Check that the response is OK:
    string http_version;
    unsigned int status_code;
    s >> http_version >> status_code;
    string status_message;
    getline(s, status_message);
    if (!s || http_version.substr(0, 5) != "HTTP/")
        throw runtime_error{ "Invalid response\n" };
    if (status_code != 200)
        throw runtime_error{ "Response returned with status code" };
    // Discard the response headers, which are terminated by a blank line:
    string header;
    while (getline(s, header) && header != "\r")
        ;
}
int main()
{
    try {
        string server = "www.stroustrup.com";
        boost::asio::ip::tcp::iostream s{ server, "http" };  // make a connection
        connect_to_file(s, server, "C++.html");    // check and open file
        regex pat{ R"((http://)?www([./#\+-]\w*)+)" }; // URL
        for (auto x : get_strings(s, pat))    // look for URLs
            cout << x << '\n';
    }
    catch (std::exception& e) {
        std::cout << "Exception: " << e.what() << "\n";
        return 1;
    }
}
```
Let's show Stroustrup what **small and readable** program actually is.
1. Download `http://www.stroustrup.com/C++.html`
2. List all links:
```
 http://www-h.eng.cam.ac.uk/help/tpl/languages/C++.html
 http://www.accu.org
 http://www.artima.co/cppsource
 http://www.boost.org
 ...
```
You can use any language, but no third-party libraries are allowed.
# Winner
[C++ answer](https://codegolf.stackexchange.com/a/44344) won by votes, but it relies on a semi-third-party library (which is disallowed by rules), and, along with another close competitor [Bash](https://codegolf.stackexchange.com/a/44317), relies on a hacked together HTTP client (it won't work with HTTPS, gzip, redirects etc.). So [**Wolfram**](https://codegolf.stackexchange.com/a/44316) is a clear winner. Another solution which comes close in terms of size and readability is [PowerShell](https://codegolf.stackexchange.com/a/44331) (with improvement from comments), but it hasn't received much attention. Mainstream languages ([Python](https://codegolf.stackexchange.com/a/44279), [C#](https://codegolf.stackexchange.com/a/44283)) came pretty close too.
# Comment on subjectivity
The most upvoted answer (C++) at the moment votes were counted was **disqualified for not following the rules** listed here in the question. There's nothing subjective about it. The second most upvoted question at that moment (Wolfram) was declared winner. The third answer's (Bash) "disqualification" is subjective, but it's **irrelevant** as the answer didn't win by votes at any point in time. You can consider it a comment if you like. Furthermore, right now the Wolfram answer is the most upvoted answer, the accepted one, and is declared winner, which means there's zero debate or subjectivity.
  
[Answer]
# Wolfram
This feels like complete cheating
```
Import["http://www.stroustrup.com/C++.html", "Hyperlinks"]
```
So just add some honest parsing on top
```
Cases[
 Import["http://www.stroustrup.com/C++.html", "XMLObject"],
 XMLElement["a", {___, "href" -> link_, ___}, ___] :> 
  link /; StringMatchQ[link, RegularExpression["((http://)?www([./#\\+-]\\w*)+)"]]
, Infinity]
```
[Answer]
# C++
```
#include <boost/asio.hpp>
#include <regex>
#include <iostream>
int main() {
    std::string server = "www.stroustrup.com";
    std::string request = "GET http://" + server + "/C++.html HTTP/1.0\r\nHost: " + server + "\r\n\r\n";
    boost::asio::ip::tcp::iostream s{server, "http"};
    s << request;
    std::regex pat{R"((http://)?www([./#\+-]\w*)+)"};
    std::smatch m;
    for (std::string l; getline(s, l);)
        if (std::regex_search(l, m, pat))
            std::cout << m[0] << "\n";
}
```
The main shortcoming is the awkward nature of boost::asio, I'm sure it can be even shorter with a better library.
[Answer]
# Pure Bash on Linux/OS X (no external utilities)
HTTP client software is notoriously bloated. We don't want those kinds of dependencies. Instead we can push the appropriate headers down a TCP stream and read the result. No need to call archaic utilities like grep or sed to parse the result.
```
domain="www.stroustrup.com"
path="C++.html"
exec 3<> /dev/tcp/$domain/80
printf "GET /$path HTTP/1.1\r\nhost: %s\r\nConnection: close\r\n\r\n" "$domain" >&3
while read -u3; do
    if [[ "$REPLY" =~ http://[^\"]* ]]; then
        printf '%s\n' "$BASH_REMATCH"
    fi
done
```
Meh - I suppose it could be more readable...
[Answer]
# Python 2
```
import urllib2 as u, re
s = "http://www.stroustrup.com/C++.html"
w = u.urlopen(s)
h = w.read()
l = re.findall('"((http)s?://.*?)"', h)
print l
```
Lame, but works
[Answer]
# C#
```
using System;
using System.Net;
using System.Text.RegularExpressions;
string html = new WebClient().DownloadString("http://www.stroustrup.com/C++.html");
foreach (Match match in Regex.Matches(html, @"https?://[^""]+"))
    Console.WriteLine(match);
```
[Answer]
# "No third-party" is a fallacy
I think the "no third-party" assumption is a fallacy. And is a specific fallacy that afflicts C++ developers, since it's so hard to make reusable code in C++. When you are developing anything at all, even if it's a small script, you will always make use of whatever pieces of reusable code are available to you.
The thing is, in languages like Perl, Python, Ruby (to name a few), reusing someone else's code is not only easy, but it is how most people actually write code most of the time.
C++, with its nearly impossible-to-maintain-compatible-ABI-requirements makes that a much tougher job, you end up with a project like Boost, which is a monstrous repository of code and very little composability outside it.
## A CPAN example
Just for the fun of it, here goes a CPAN-based example, with proper parsing of the html, instead of [trying to use regex to parse html](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)
```
#!/usr/bin/perl
use HTML::LinkExtor;
sub callback {
   my ($tag, %links) = @_;
   print map { "$_\n" } values %links
}
$p = HTML::LinkExtor->new(\&callback, "http://www.stroustrup.com/C++.html");
```
[Answer]
# UNIX shell
```
lynx -dump http://www.stroustrup.com/C++.html | grep -o '\w*://.*'
```
Also finds an `ftp://` link :)
Another way, without relying on `://` syntax:
```
lynx -dump -listonly http://www.stroustrup.com/C++.html | sed -n 's/^[ 0-9.]\+//p'
```
[Answer]
# CSS 3
```
* {
  margin: 0;
  padding: 0;
}
*:not(a) {
  font: 0/0 monospace;
  color: transparent;
  background: transparent !important;
}
a {
  content: "";
}
a[href*="://"]::after {
  content: attr(href);
  float: left;
  clear: left;
  display: block;
  font: 12px monospace;
  color: black;
}
```
This code can be used as a user style to display only absolute links on a page in an unformatted list. It may not work correctly if your browser enforces minimum font size.
It works correctly with `http://www.stroustrup.com/C++.html` (note `!important` on `background`). In order to work on other pages with more styles, it must be extended (reset more properties, mark properties as important etc.).
Alternative version which includes relative links except intrapage links starting with hashes (it relies on a hard-coded absolute link, unfortunately):
```
* {
  margin: 0;
  padding: 0;
}
*:not(a) {
  font: 0/0 monospace;
  color: transparent;
  background: transparent !important;
  float: none !important;
  width: auto !important;
  border: none !important;
}
a {
  content: "";
}
a::after {
  display: none;
}
a:not([href^="#"])::after {
  content: attr(href);
  float: left;
  clear: left;
  display: block;
  font: 12px monospace;
  color: black;
}
a:not([href*="://"])::after {
  content: "http://www.stroustrup.com/" attr(href);
}
```
[Answer]
# Clojure
```
(->> (slurp "http://www.stroustrup.com")
     (re-seq #"(?:http://)?www(?:[./#\+-]\w*)+"))
```
[Answer]
## Emacs Lisp
```
(with-current-buffer (url-retrieve-synchronously "http://www.stroustrup.com/C++.html")
  (while (re-search-forward "https?://[^\\\"]*")
    (print (match-string 0))))
```
[Answer]
**Scala**
```
"""\"(https?://.*?)\"""".r.findAllIn(scala.io.Source.fromURL("http://www.stroustrup.com/C++.html").mkString).foreach(println)
```
[Answer]
# PHP 5
```
<?php
preg_match_all('/"(https?:\/\/.*?)"/',file_get_contents('http://www.stroustrup.com/C++.html'),$m);
print_r($m[1]);
```
[Answer]
# D
```
import std.net.curl, std.stdio;
import std.algorithm, std.regex;
void main() {
foreach(_;byLine("http://www.stroustrup.com/C++.html")
    .map!((a)=>a.matchAll(regex(`<a.*?href="(.*)"`)))
    .filter!("a")){ writeln(_.front[1]); }
}
```
[Answer]
## PowerShell
Text search for all fully-qualified [URLs](http://en.wikipedia.org/wiki/Uniform_resource_locator) (including JavaScript, CSS, etc.):
```
[string[]][regex]::Matches((iwr "http://www.stroustrup.com/C++.html"), '\w+://[^"]+')
```
Or to get links in anchor tags only (includes relative URLs):
```
(iwr "http://www.stroustrup.com/C++.html").Links | %{ $_.href }
```
Shorter versions from comments:
```
(iwr "http://www.stroustrup.com/C++.html").Links.href
```
```
(iwr "http://www.stroustrup.com/C++.html").Links.href-match":"
```
[Answer]
# Haskell
Some troubles with `"\w"` in Text.Regex.Posix
```
import Network.HTTP
import Text.Regex.Posix
pattern = "((http://)?www([./#\\+-][a-zA-Z]*)+)"
site = "http://www.stroustrup.com/C++.html"
main = do
    file <- getResponseBody =<< simpleHTTP (getRequest site)
    let result = getAllTextMatches $ file =~ pattern
    putStr $ unlines result -- looks nicer
```
[Answer]
# Node.js
```
var http = require('http');
http.get('http://www.stroustrup.com/C++.html', function (res) {
    var data = '';
    res.on('data', function (d) {
        data += d;
    }).on('end', function () {
        console.log(data.match(/"https?:\/\/.*?"/g));
    }).setEncoding('utf8');
});
```
[Answer]
# Ruby
```
require 'net/http'
result = Net::HTTP.get(URI.parse('http://www.stroustrup.com/C++.html'))
result.scan(/"((http)s?://.*?)"/)
```
[Answer]
# PHP
As far as I can tell, most modern PHP installations come with DOM processing, so here's one that actually traverses the anchors inside the HTML:
```
foreach (@DOMDocument::loadHTMLFile('http://stroustrup.com/C++.html')->getElementsByTagName('a') as $a) {
    if (in_array(parse_url($url = $a->getAttribute('href'), PHP_URL_SCHEME), ['http', 'https'], true)) {
        echo $url, PHP_EOL;
    }
}
```
The inner loop could be shortened to:
```
preg_match('~^https?://~', $url = $a->getAttribute('href')) && printf("%s\n", $url);
```
[Answer]
# Unix Shell
```
wget -q -O - http://www.stroustrup.com/C++.html | sed -n '/http:/s/.*href="\([^"]*\)".*/\1/p' | sort
```
Though i have to admit this doesn't work if there's more than one link on a line.
[Answer]
# Java
```
import java.util.regex.*;
class M{
    public static void main(String[]v)throws Throwable{
        Matcher m = Pattern.compile( "\"((http)s?://.*?)\"" )
            .matcher(
                 new Scanner(
                         new URL( "http://www.stroustrup.com/C++.html" )
                             .openStream(),
                         "UTF-8")
                     .useDelimiter("\\A")
                     .next());
        while(m.find())
            System.out.println(m.group());
    }
}
```
[Answer]
# Groovy
```
"http://www.stroustrup.com/C++.html".toURL().text.findAll(/https?:\/\/[^"]+/).each{println it}
```
[Answer]
# SQL (SQL Anywhere 16)
## Define a stored procedure to fetch the web page
```
CREATE OR REPLACE PROCEDURE CPPWebPage()
URL 'http://www.stroustrup.com/C++.html'
TYPE 'HTTP';
```
## Produce the result set using a single query
```
SELECT REGEXP_SUBSTR(Value,'"https?://[^""]+"',1,row_num) AS Link  
FROM (SELECT Value FROM CPPWebPage() WITH (Attribute LONG VARCHAR, Value LONG VARCHAR) 
      WHERE Attribute = 'Body') WebPage, 
      sa_rowgenerator( 1, 256 ) 
WHERE Link IS NOT NULL;
```
Limitations: This produces up to 256 links. If more links exist, then bump up the 256 to an appropriate value.
[Answer]
# CoffeeScript / NodeJS
```
require('http').get 'http://www.stroustrup.com/C++.html', (r) ->
    dt = '';
    r.on 'data', (d) -> dt += d
    r.on 'end' , (d) -> console.log dt.match /"((http)s?:\/\/.*?)"/g
    
```
[Answer]
## Perl
```
use LWP;
use feature 'say';
my $agent = new LWP::UserAgent();
my $response = $agent->get('http://www.stroustrup.com/C++.html');
say for $response->content =~ m<"(https?://.+?)">g;
```
[Answer]
# R
```
html<-paste(readLines("http://www.stroustrup.com/C++.html"),collapse="\n")
regmatches(html,gregexpr("http[^([:blank:]|\\\"|<|&|#\n\r)]+",html))
```
...although R is written mainly in C... so probably a few lines of C code behind those 2 lines of R code.
[Answer]
**Objective-C**
```
NSString *s;
for (id m in [[NSRegularExpression regularExpressionWithPattern:@"\"((http)s?://.*?)\"" options:0 error:nil] matchesInString:(s=[NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.stroustrup.com/C++.html"]])]){
    NSLog(@"%@",[s substringWithRange:[m range]]);
}
```
[Answer]
## Tcl
```
package require http
set html [http::data [http::geturl http://www.stroustrup.com/C++.html]]
puts [join [regexp -inline -all {(?:http://)?www(?:[./#\+-]\w*)+} $html] \n]
```
[Answer]
# Go
```
package main
import (
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "regexp"
)
func main() {
    resp, err := http.Get("http://www.stroustrup.com/C++.html")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    defer resp.Body.Close()
    data, _ := ioutil.ReadAll(resp.Body)
    results := regexp.MustCompile(`https?://[^""]+`).FindAll(data, -1)
    for _, row := range results {
        fmt.Println(string(row))
    }
}
```
**P.S.** this code reads entire source into memory, so consider using `regexp.FindReaderIndex` to search in stream, that'll make the app bulletproof.
[Answer]
# CJam
CJam does not have regex so I had to use a different approach in this one:
```
"http://www.stroustrup.com/C++.html"g''/'"*'"/(;2%{_"http://"#!\"https://"#!e|},N*
```
I first convert all `'` to `"`, then I split on all `"`, take every alternative string and then finally filter that list for strings starting with `http://` or `https://`. After that, simply print each filtered string on a new line.
Try it using the [Java interpreter](http://sourceforge.net/p/cjam/wiki/Home/) like
```
java -jar cjam-0.6.2.jar file.cjam
```
where file.cjam has the contents of the code above.
[Answer]
# F#
This code could be far shorter but I would write something like this if I ever expected to have to read or use this code again so it has many unnecessary type annotations. It demonstrates the use of an active pattern *MatchValue* to enable pattern-matching against the standard CLR type *Match*
```
open System.Net
let (|MatchValue|) (reMatch: Match) : string = reMatch.Value
let getHtml (uri : string) : string = 
    use webClient = WebClient() in
        let html : string = webClient.DownloadString(uri)
        html
let getLinks (uri : string) : string list =
    let html : string = getHtml uri
    let matches : MatchCollection = Regex.Matches(html, @"https?://[^""]+") 
    let links = [ for MatchValue reMatch in matches do yield reMatch ]
    links
let links = getLinks "http://www.stroustrup.com/C++.html" 
for link in links do
    Console.WriteLine(link)
```
**Edit**
I made getLinks its own function
]