Skip to content

Commit

Permalink
regex sketch balanced groups
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Dec 10, 2023
1 parent f5efdaf commit e51f3c0
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
120 changes: 120 additions & 0 deletions Pwsh/Regex/Using-BalancedGroupDefintions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### about\n",
"\n",
"Using Regex's with Balanced groups"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\u001b[32;1mModuleType\u001b[0m\u001b[32;1m Version \u001b[0m \u001b[32;1;3mPreRelease\u001b[0m\u001b[32;1m Name \u001b[0m\u001b[32;1m ExportedCommands\u001b[0m\n",
"\u001b[32;1m----------\u001b[0m \u001b[32;1m------- \u001b[0m \u001b[32;1m----------\u001b[0m \u001b[32;1m---- \u001b[0m \u001b[32;1m----------------\u001b[0m\n",
"Script 0.7.8 irregular {Compress-Regex, Export-RegEx…\n",
"( path:\"c:\\foo\" && ( ext:ps1 dm:last2days ) )\n",
"\n"
]
}
],
"source": [
"Import-module irregular -passThru\n",
"$re = @{}\n",
"$Str = @'\n",
"( path:\"c:\\foo\" && ( ext:ps1 dm:last2days ) )\n",
"'@\n",
"\n",
"$Str"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"dotnet_interactive": {
"language": "pwsh"
},
"polyglot_notebook": {
"kernelName": "pwsh"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(?x)\r\n",
"^\r\n",
"# any of neither balanced character\r\n",
"[^()]*\r\n",
"\r\n",
"# open: lefty parens, closing: righty parens\r\n",
"\\(\r\n",
"(?<Open>.*)\r\n",
"\\)\r\n",
"\r\n",
"$\r\n"
]
}
],
"source": [
"$re.Balance = @'\n",
"(?x)\n",
" ^\n",
" # any of neither balanced character\n",
" [^()]*\n",
"\n",
" # open: lefty parens, closing: righty parens\n",
" \\(\n",
" (?<Open>.*)\n",
" \\)\n",
"\n",
" $\n",
"'@\n",
"\n",
"$re.Balance"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (PowerShell)",
"language": "PowerShell",
"name": ".net-pwsh"
},
"language_info": {
"name": "polyglot-notebook"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "pwsh",
"items": [
{
"aliases": [],
"languageName": "pwsh",
"name": "pwsh"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
64 changes: 64 additions & 0 deletions Pwsh/Regex/Using-BalancedGroupDefintions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Import-Module irregular
# new-Regex -Pattern (New-Regex -NotLiteralCharacter '(', ')') -Repeat *
$Re = @{}
$Ex = @{
First = '( ext:ps1 ( dm:last2weeks ))'
}

$re.scratch = @'
(?x)
^
# any of neither balanced character
[^()]*
# open: lefty parens, closing: righty parens
\(
(?<Open>.*)
\)
$
'@

$re.Balance = @'
(?x)
^
# https://regex101.com/r/dTWnVb/1
# example: https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions#balancing-group-definitions
# any-non of neither balanced character
[^()]*
# open: lefty parens, closing: righty parens
(?<Open> \( )
# any-non
[^()]*
(
(?<Open> \( )
# any-non
[^()]*
)+
# Match a right angle bracket, assign the substring between the Open group and the current group to the Close group, and delete the definition of the Open group.
#
(?<Close-Open> \) )
[^()]*
(
(?<Close-Open> \) )
[^()]*
)+
(
(
(?<Open> \( )
[^<>]*
)+
(
(?<Close-Open> \) )
[^()]*
)+
)*
(?(Open)(?!))
$
'@
$re.Balance

0 comments on commit e51f3c0

Please sign in to comment.