Hot answers tagged

145

Python 3, 32 bytes eval(input().translate(")("*50)) Outputs via exit code: Error for false, no error for True. The string is evaluated as Python code, replacing parens ( for a and ) for b. Only expressions of the form a^n b^n become well-formed expressions of parentheses like ((())), evaluating to the tuple (). Any mismatched parens give an error, as ...


127

oOo, 1569 1515 bytes Had to be done. Try it here. Golfed: ...


116

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*)+)"]] , ...


115

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://)?...


85

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


65

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


63

Pure Bash (no utilities), 200 bytes - 40 bonus = 160 while read u;do u=${u#*//} d=${u%%/*} exec 3<>/dev/tcp/$d/80 echo "GET /${u#*/} HTTP/1.1 host:$d Connection:close ">&3 mapfile -tu3 A a=${A[@]} a=${a#*<p>} a=${a%</p>*} echo "${a//<\/p>*<p>/" "}" done I think this is up to the spec, though of course watch out for ...


55

C# using System; using System.Net; using System.Text.RegularExpressions; class Program { static void Main() { string html = new WebClient().DownloadString("http://www.stroustrup.com/C++.html"); foreach (Match match in Regex.Matches(html, @"https?://[^""]+")) Console.WriteLine(match); } }


53

"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, ...


47

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'


43

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


43

CJam, 18 17 13 bytes Thanks to aditsu for saving 4 bytes. qW:O%eu~"eo"= Try the test suite here. (The test suite is too long for the permalink. Just copy them from the challenge spec.) Explanation q e# Read the input. W:O e# Push a -1 and store it in variable O. % e# Use the -1 to reverse the string, because CJam's stack-based nature and the ...


40

C There are only so many Roman numerals, since 4000 and higher have no standard notation, and the preprocessor is a wonderful decompression tool, especially if you have no problems with the fact that the code has undefined behaviour. enum{_ #define a_ #define d_ #define g_ #define j_ #define a(x)c(x)b #define b(x)c(x)a #define c(x)x, #define d(x)f(x)e #...


39

Python 2, 332 * 0.6 * 0.8 = 159.36 import os from subprocess import* from tempfile import* f,n,a=NamedTemporaryFile(delete=0),open(os.devnull,'w'),'' f.write(os.read(0,256)) f.close() for l in["CJam","java","-jar","cjam.jar"],["Pyth","./pyth.py"],["Golfscript","./golfscript.rb"]:a+=(l[0]+' ')*(call(args=l[1:]+[f.name],stdout=n,stderr=n)>0) print a or'...


36

Clojure (->> (slurp "http://www.stroustrup.com") (re-seq #"(?:http://)?www(?:[./#\+-]\w*)+"))


34

MATL, 5 4 bytes tSP- Prints a non-empty array of 1s if the string belongs to L, and an empty array or an array with 0s (both falsy) otherwise. Thanks to @LuisMendo for golfing off 1 byte! Try it online! How it works t Push a copy of the implicitly read input. S Sort the copy. P Reverse the sorted copy. - Take the difference of the ...


32

Shell + coreutils, 6 This answer becomes invalid if an encoding other than UTF-8 is used. wc -mc Test output: $ printf '%s' "(~R∊R∘.×R)/R←1↓ιR" | ./count.sh 17 27 $ In case the output format is strictly enforced (just one space separating the the two integers), then we can do this: Shell + coreutils, 12 echo`wc -mc` Thanks to @immibis for ...


29

Emacs Lisp (with-current-buffer (url-retrieve-synchronously "http://www.stroustrup.com/C++.html") (while (re-search-forward "https?://[^\\\"]*") (print (match-string 0))))


28

Scala """\"(https?://.*?)\"""".r.findAllIn(scala.io.Source.fromURL("http://www.stroustrup.com/C++.html").mkString).foreach(println)


28

Vim, 54, 52, 49 47 keystrokes 2wa0<esc>qqYp<c-a>6ldf @qq@q$dT]dd:%norm dwf{xwC;<CR>gg"0P Explanation: 2wa0<esc> 'Move 2 words forward, and insert a 0. qq 'Start recording in register Q Yp 'Duplicate the line <c-a>6l 'Increment the ...


28

Retina, 12 bytes Credits to FryAmTheEggman who found this solution independently. +`a;?b ; ^;$ Prints 1 for valid input and 0 otherwise. Try it online! (The first line enables a linefeed-separated test suite.) Explanation Balancing groups require expensive syntax, so instead I'm trying to reduce a valid input to a simple form. Stage 1 +`a;?b ; The + ...


27

Pyth, 16 14 bytes @"eo".vjdXzGU9 Pyth can itself evaluate a string, that is in Pyth syntax. Therefore I replace e and o with 4 and 5. Then the evaluation will give me an even or odd number, and I can easily print the result. Try it online: Demonstration or Test Suite Explanation: @"eo".vjdXzGU9 implicit: z = input string XzGU9 replace "e" ...


26

Common Lisp - 2524 1890 bytes (defun f(i)(let((s(loop as r in i collect(g r)))(n())(output""))(loop until n do(setf n T)(loop as r in s do(if(cdr r)(progn(setf output(c output(e r))(cdr r)(cdr(cdr r)))(setf n()))(setf output(c output(b(car r))))))(setf output(c output(format()"~%"))))output))(defun g(r)(if(stringp r)(d(m(length r))r)(if(<(length r)2)(d(m(...


25

PHP 5 <?php preg_match_all('/"(https?:\/\/.*?)"/',file_get_contents('http://www.stroustrup.com/C++.html'),$m); print_r($m[1]);


25

PowerShell Text search for all fully-qualified URLs (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....


25

Haskell, 31 bytes f s=s==[c|c<-"ab",'a'<-s]&&s>"" The list comprehension [c|c<-"ab",'a'<-s] makes a string of one 'a' for each 'a' in s, followed by one 'b' for each 'a' in s. It avoids counting by matching on a constant and producing an output for each match. This string is checked to be equal to the original string, and the ...


22

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'); });


22

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]); } }


22

Javascript, 11 bytes a=>b=>+a==b Abusing Javascript's casting rules a bit; +a coerces a into a numeric type. -6 bytes thanks to Shaggy and Martin Ender♦ Also a cool take by LiefdeWen: a=>b=>~~a==~~b


22

x86 (32-bit) machine code, 59 57 bytes This function takes esi as a pointer to a null-terminated string and returns the value in edx. (Listing below is GAS input in AT&T syntax.) .globl parse_cxx14_int .text parse_cxx14_int: push $10 pop %ecx # store 10 as base xor %eax,%eax # initialize ...


Only top voted, non community-wiki answers of a minimum length are eligible