How to use php pattern Regex in Delphi -
most of time, when regex pattern php code, , need "traslate" delphi. big problem in delphi php escapes doesn't works in delphi, if ignore them of time ok. not in next example, if remove /x pattern, function preg_match_all doesn't output value.
this code in php:
$pattern = "/\n". "\s(counters?\\([^)]*\\))|\n". "\a(counters?\\([^)]*\\))|\n". "\s([\"']) ( (?:[^\"']|\\\\[\"'])+ )(?<!\\\\)\\3|\n". "\a([\"']) ( (?:[^\"']|\\\\[\"'])+ )(?<!\\\\)\\5|\n" . "\s([^\s\"']+)|\n" . "\a([^\s\"']+)\n". "/xi"; preg_match_all($pattern, '"file " counter(file)', $matches, preg_set_order);
this output:
array (size=2) 0 => array (size=7) 0 => string '"file "' (length=7) 1 => string '' (length=0) 2 => string '' (length=0) 3 => string '' (length=0) 4 => string '' (length=0) 5 => string '"' (length=1) 6 => string 'file ' (length=5) 1 => array (size=2) 0 => string ' counter(file)' (length=14)
this i've done in delphi:
type tmatches = array of array of string; var matches: tmatches; pattern := '/\n'+ '\s(counters?\\([^)]*\\))|\n'+ '\a(counters?\\([^)]*\\))|\n'+ '\s([\"'']) ( (?:[^\"'']|\\\\[\"''])+ )(?<!\\\\)\\3|\n'+ '\a([\"'']) ( (?:[^\"'']|\\\\[\"''])+ )(?<!\\\\)\\5|\n'+ '\s([^\s\"'']+)|\n'+ '\a([^\s\"'']+)\n'+ '/xi'; regexmatchall(pattern,'"page " counter(page)',matches);
the function regexmatchall here , works in of case:
procedure regexmatchall(pattern: string; subject: string; out matches: tmatches); var d, sd: integer; regex: tregex; reggroupcoll: tgroupcollection; regcoll: tmatchcollection; begin regex := tregex.create(pattern); regcoll := regex.matches(subject); setlength(matches, regcoll.count); // numero de coincidencias [array [x]] d := 0 regcoll.count - 1 begin reggroupcoll := regcoll.item[d].groups; setlength(matches[d], reggroupcoll.count); // numero de grupos [array [d][sd]] sd := 0 reggroupcoll.count - 1 matches[d][sd] := reggroupcoll.item[sd].value; end; end;
also alter \n #13#10, , doesn't matter, matches array empty.i know if hard pattern regex, if @ lastly can resolve issue, wonderful, there lot of php regex code , if can know how utilize in delphi going great delphi developers.
as far can see, php's regex back upwards built on pcre. delphi's appear using. although i'm guessing there because state this. however, think it's reasonably safe assumption.
the x
modifier in php corresponds pcre_extended
flag. in delphi corresponding setting roignorepatternspace
option. pass alternative when phone call matches
.
one aside. please don't post of import code using off-site links. should not have leave question larn code is. reader should able info direct question. is, reader wonder how reply discerns delphi regex library use, , calling matches
.
php regex delphi
No comments:
Post a Comment