Last active
December 23, 2017 09:51
-
-
Save adriannier/b3ebd3bcb432b89aaa0af176fda933a7 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
set thePatterns to "/(r\\s*\\d\\s*\\d\\s*[-\\s]*\\d\\s*\\d\\s*\\d\\s*\\d\\s*\\d)/i" | |
set theStrings to "R17-11517 r17-11518 r17-11519 R17-11518 R17-11519" | |
repeat 7 times | |
set thePatterns to thePatterns & return & thePatterns | |
set theStrings to theStrings & return & theStrings | |
end repeat | |
set startDate to current date | |
set theResult to matchesForPatternsAndStrings(thePatterns, theStrings) | |
log "Duration: " & ((current date) - startDate) & "s" | |
log "Processed " & (count of paragraphs of thePatterns) * (count of paragraphs of theStrings) & " regular expressions" | |
log "Result length: " & length of theResult & " characters" | |
-- Functions | |
on matchesForPatternsAndStrings(thePatterns, theStrings) | |
set phpSource to " | |
error_reporting(0); | |
$allLines = explode(chr(13), $argv[1]); | |
$allRegex = explode(chr(13), $argv[2]); | |
$allMatches = array(); | |
foreach ($allLines as &$thisLine) { | |
$allMatchesForThisLine = array(); | |
foreach ($allRegex as &$regex) { | |
$matches = allRegexMatches($regex, $thisLine); | |
if (count($matches) > 0) { | |
$allMatchesForThisLine = array_merge($allMatchesForThisLine, $matches); | |
} | |
} | |
$allMatchesForThisLineAsString = implode('<DELIMITER>', $allMatchesForThisLine); | |
array_push($allMatches, $allMatchesForThisLineAsString); | |
} | |
$allMatchesAsString = implode(\"\\n\", $allMatches); | |
echo($allMatchesAsString); | |
function allRegexMatches($pattern, $text) { | |
@preg_match_all($pattern, $text, $matches, PREG_SET_ORDER); | |
if ($matches === NULL) { | |
return array('?'); | |
} | |
$allMatches = array(); | |
for ($i = 0; $i < count($matches); $i++ ) { | |
if (isset($matches[$i][1]) && trim($matches[$i][1]) != '') { | |
$allMatches[] = trim($matches[$i][1]); | |
} | |
} | |
return $allMatches; | |
}" | |
return do shell script "/usr/bin/php -r " & quoted form of phpSource & " " & quoted form of theStrings & " " & quoted form of thePatterns | |
end matchesForPatternsAndStrings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment