Skip to content

Instantly share code, notes, and snippets.

@r-plus
Created June 9, 2014 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-plus/304fefcad63834ded16e to your computer and use it in GitHub Desktop.
Save r-plus/304fefcad63834ded16e to your computer and use it in GitHub Desktop.
Ordered regexp matching code with PowerShell v3.0+
$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