-
-
Save sledorze/6164205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VerEx | |
{ | |
public static function main() | |
{ | |
var test = new VerEx() | |
.startOfLine() | |
.then("http") | |
.maybe("s") | |
.then("://") | |
.maybe("www.") | |
.anythingBut(" ") | |
.endOfLine(); | |
trace(test.match("https://www.google.com")); | |
} | |
static var escape = ~/[^\w]/g; | |
var exp:String; | |
var opt:String; | |
var prefix:String; | |
var suffix:String; | |
var reg:EReg; | |
public function new() | |
{ | |
exp = prefix = suffix = ""; | |
opt = "gm"; | |
} | |
function compile() | |
{ | |
reg = new EReg(prefix + exp + suffix, opt); | |
} | |
inline function add(v:String) | |
{ | |
exp += v; | |
compile(); | |
} | |
inline function setOpt(v:String, b:Bool) | |
{ | |
opt = opt.split(v).join(b ? v : ''); | |
compile(); | |
} | |
inline function sanitize(v:String) | |
{ | |
return escape.map(v, function(e) return '\\' + e.matched(0)); | |
} | |
public function startOfLine(?enable=true) | |
{ | |
prefix = enable ? '^' : ''; | |
compile(); | |
return this; | |
} | |
public function endOfLine(?enable=true) | |
{ | |
suffix = enable ? '$' : ''; | |
compile(); | |
return this; | |
} | |
public function then(value:String) | |
{ | |
value = sanitize(value); | |
add('(?:$value)'); | |
return this; | |
} | |
inline public function find(value:String) | |
{ | |
return then(value); | |
} | |
public function maybe(value:String) | |
{ | |
value = sanitize(value); | |
add('(?:$value)?'); | |
return this; | |
} | |
public function anything() | |
{ | |
add('(?:.*)'); | |
return this; | |
} | |
public function anythingBut(value:String) | |
{ | |
value = sanitize(value); | |
add('(?:[^$value]*)'); | |
return this; | |
} | |
public function something() | |
{ | |
add('(?:.+)'); | |
return this; | |
} | |
public function somethingBut(value:String) | |
{ | |
value = sanitize(value); | |
add('(?:[^$value]+)'); | |
return this; | |
} | |
public function lineBreak() | |
{ | |
add('(?:(?:\\n)|(?:\\r\\n))'); | |
return this; | |
} | |
public function tab() | |
{ | |
add('\\t'); | |
return this; | |
} | |
public function word() | |
{ | |
add('\\w+'); | |
return this; | |
} | |
public function anyOf(value:String) | |
{ | |
value = sanitize(value); | |
add('[$value]'); | |
return this; | |
} | |
inline public function any(value:String) | |
{ | |
return anyOf(value); | |
} | |
public function range(from:String, to:String) | |
{ | |
from = sanitize(from); | |
to = sanitize(to); | |
add('[$from-$to]'); | |
return this; | |
} | |
public function withAnyCase(?enable=true) | |
{ | |
setOpt('i', enable); | |
return this; | |
} | |
public function stopAtFirst(?enable=true) | |
{ | |
setOpt('g', !enable); | |
return this; | |
} | |
public function searchOneLine(?enable=true) | |
{ | |
setOpt('m', !enable); | |
return this; | |
} | |
public function or(value:String) | |
{ | |
prefix += '(?:'; | |
suffix = ')' + suffix; | |
add(')|(?:'); | |
if (value != null) then(value); | |
return this; | |
} | |
public function beginCapture() | |
{ | |
// add the end of the capture group to the suffix for now so | |
// compilation continues to work | |
suffix += ")"; | |
add("("); | |
return this; | |
} | |
public function endCapture() | |
{ | |
// remove the last parentheses from the suffix and add to the | |
// regex itself | |
suffix = suffix.substring(0, suffix.length - 1); | |
add(")"); | |
return this; | |
} | |
// EReg API | |
inline public function map(s:String, f:EReg -> String) | |
return reg.map(s, f); | |
inline public function match(s:String) | |
return reg.match(s); | |
inline public function matchSub(s:String, pos:Int, len:Int) | |
return reg.matchSub(s, pos, len); | |
inline public function matched(n:Int) | |
return reg.matched(n); | |
inline public function matchedLeft() | |
return reg.matchedLeft(); | |
inline public function matchedRight() | |
return reg.matchedRight(); | |
inline public function matchedPos() | |
return reg.matchedPos(); | |
inline public function replace(s:String, by:String) | |
return reg.replace(s, by); | |
inline public function split(s:String) | |
return reg.split(s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment