Created
June 9, 2014 14:18
-
-
Save r-plus/304fefcad63834ded16e to your computer and use it in GitHub Desktop.
Ordered regexp matching code with PowerShell v3.0+
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
$json = @" | |
{ | |
"matcher": | |
[ | |
{ | |
"regexp" : "^re$", | |
"dest" : "dest1" | |
}, | |
{ | |
"regexp" : "^h\\w+eほげ.{3}我$", | |
"dest" : "dest2" | |
}, | |
{ | |
"regexp" : ".*", | |
"dest" : "dest3" | |
} | |
] | |
} | |
"@ | ConvertFrom-Json | |
# ConvertFrom-Json requred PowerShell version 3.0 or later. | |
$string = "ho | |
geほげフガ負我" | |
# chomp | |
$string = $string -replace "`t|`n|`r","" | |
# matching | |
$json.matcher | % { | |
echo "trying $($_.regexp) ..." | |
if ($string -cmatch $_.regexp) { | |
echo hit! | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment