Patch for regex.d, enables (?:) regex syntax as per ECMA262
text/plain
1540
Comments
Comment #0 by dmitry.olsh — 2010-11-05T09:35:15Z
Intro: Non-capturing parentheses group the regex so you can apply regex operators, but do not capture anything and do not create backreferences.
Examples:
//A very dumb example, matches abcabcabc, no backrefs created
(?:abc){3}
//A decent attempt to snatch href field of <a> html tag, without unnessary
//backrefs:
<(?:a|A)(?:[^<>]*)href *= *"?([^"<> ]*)"?(?:[^<>]*)>
Rationale: ECMA262 standart mentioned on http://www.digitalmars.com/d/2.0/phobos/std_regex.html
requires support of such construct. Sooner or later we should get rid of
"however, some of the very advanced forms may behave slightly differently", also given the fact that sometimes it's simple. See attached patch.
Backtracking is also costly, see benchmark code/results
(uses the proposed patch):
//===bench.d===
import std.regex, std.stdio,std.datetime;
void main(){
auto r1 = regex(`(?:a|A)(?:[^<>]*)href *= *"?([^"<> ]*)"?(?:[^<>]*)>`,"g");
auto r2 = regex(`(a|A)([^<>]*)href *= *"?([^"<> ]*)"?([^<>]*)>`,"g");
void nobackref(){
match(`<a href = http://www.google.com id="G"/>`,r1).hit;
}
void backref(){
match(`<a href = http://www.google.com id="G"/>`,r2).hit;
}
auto bench = benchmark!(nobackref,backref)(1_000);
writeln("No backref: ",bench[0].milliseconds);
writeln("With backref: ",bench[1].milliseconds);
}
//======
Results on my machine, min .. max of 10
No backref: 256.955 .. 267.341
With backref: 580.636 .. 587.187
P.S. I have rebuilt phobos (on Windows), and run unitestes, output:
C:\dmd2\src\phobos>unittest.exe
--- std.socket(660) broken test ---
(std.socket.HostException: Address family mismatch)
9abc5a5a12345678
args.length = 1
args[0] = 'C:\dmd2\src\phobos\unittest.exe~T'
Vendor string: AuthenticAMD
Processor string: AMD Phenom(tm) II X4 940 Processor
Signature: Family=16 Model=4 Stepping=2
Features: MMX FXSR SSE SSE2 SSE3 3DNow! 3DNow!+ MMX+ AMD64 HTT
Multithreading: 4 threads / 4 cores
Success!
Comment #1 by dmitry.olsh — 2010-11-05T09:36:40Z
Created attachment 800
Patch for regex.d, enables (?:) regex syntax as per ECMA262
Comment #2 by jlquinn — 2011-02-25T08:17:20Z
Changing from enhancement to a bug, as std.regex is supposed to support ECMA-262.