Debian Codegraph @

examples/regex_inverter/index.html

raw
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>PyParsing Regex Inverter</title>
7 <link rel="stylesheet" href="https://pyscript.net/releases/2025.11.1/core.css">
8 <script type="module" src="https://pyscript.net/releases/2025.11.1/core.js"></script>
9 <style>
10 body { font-family: sans-serif; margin: 2em; }
11 #regex-input { width: 300px; }
12 #output { width: 100%; height: 400px; margin-top: 1em; font-family: monospace; white-space: pre; border: 1px solid #ccc; padding: 0.5em; overflow: auto; }
13 .controls { margin-bottom: 1em; }
14 table { border-collapse: collapse; width: 100%; }
15 table th, table td { border: 1px solid #ddd; padding: 8px; text-align: left; }
16 table th { background-color: #f2f2f2; text-align: center; }
17 table.quick-ref td:first-child { text-align: center; }
18 </style>
19 </head>
20 <body>
21 <h1>Regex Inverter</h1>
22 <h4>by Paul McGuire, January 2026</h4>
23 <details>
24 <summary>Description</summary>
25 <div>
26 <p>This page allows you to invert a regular expression, generating strings that match it.</p>
27 <p><strong>Instructions:</strong> Enter a regular expression in the "Regex" field and specify the maximum number of results you want to see (up to 100,000,000). Click "Invert" or press Enter to generate the matching strings.</p>
28 <p><strong>Constraints:</strong></p>
29 <ul>
30 <li>Unbounded repetition operators <code>+</code> and <code>*</code> are <strong>not supported</strong>.</li>
31 <li>Replace <code>+</code> or <code>*</code> with explicit <code>{1,n}</code> or <code>{min,max}</code> repetition operators (e.g., use an explicit repetition like <code>[A-Z]{1,4}</code> instead of <code>[A-Z]+</code>, or <code>[A-Z]{,4}</code> instead of <code>[A-Z]*</code>).</li>
32 <li>For brevity, all generated strings in this utility are limited to 7-bit ASCII characters. By default, Python's <code>re</code> methods will match the full Unicode set, so macros like <code>\d</code> could match numeric digits in other language character sets beyond just the ASCII digits '0' through '9'.</li>
33 </ul>
34 <p><strong>Note:</strong> Complex regular expressions or those with large repetition counts may take some time to process.</p>
35 </div>
36 </details>
37 <p>
38 <details>
39 <summary>Regular Expressions Quick Reference</summary>
40 <div>
41 <table class="quick-ref">
42 <thead>
43 <tr><th>Construct</th><th>Description</th></tr>
44 </thead>
45 <tbody>
46 <tr><td><code>.</code></td><td>Any character except newline</td></tr>
47 <tr><td><code>\d</code></td><td>Digit <code>[0-9]</code></td></tr>
48 <tr><td><code>\w</code></td><td>Word (identifier) character <code>[a-zA-Z0-9_]</code></td></tr>
49 <tr><td><code>\s</code></td><td>Whitespace character</td></tr>
50 <tr><td><code>\D</code></td><td>Non-digit</td></tr>
51 <tr><td><code>\W</code></td><td>Non-word character</td></tr>
52 <tr><td><code>\S</code></td><td>Non-whitespace character</td></tr>
53 <tr><td><code>?</code></td><td>0 or 1 repetition</td></tr>
54 <tr><td><code>{n}</code></td><td>Exactly <i>n</i> repetitions</td></tr>
55 <tr><td><code>{n,m}</code></td><td>Between <i>n</i> and <i>m</i> repetitions</td></tr>
56 <tr><td><code>{,m}</code></td><td>0 to <i>m</i> repetitions</td></tr>
57 <tr><td><code>[...]</code></td><td>Character class (any of these characters)</td></tr>
58 <tr><td><code>[^...]</code></td><td>Negated character class</td></tr>
59 <tr><td><code>|</code></td><td>Alternation (OR)</td></tr>
60 <tr><td><code>(...)</code></td><td>Grouping</td></tr>
61 <tr><td><code>(?:...)</code></td><td>Grouping (non-capturing)</td></tr>
62 <tr><td><code>(?P&lt;name&gt;...)</code></td><td>Grouping (named group)</td></tr>
63 <tr><td colspan="2">Other common regex features not covered in this utility</td></tr>
64 <tr><td><code>^</code></td><td>Start of a line</td></tr>
65 <tr><td><code>$</code></td><td>End of a line</td></tr>
66 <tr><td><code>\A</code></td><td>Start of string</td></tr>
67 <tr><td><code>\Z</code></td><td>End of string</td></tr>
68 <tr><td><code>*</code></td><td>0 or more repetitions (use <code>{,m}</code> to limit repetitions)</td></tr>
69 <tr><td><code>+</code></td><td>1 or more repetitions (use <code>{1,m}</code> to limit repetitions)</td></tr>
70 <tr><td><code>\b</code></td><td>Word boundary</td></tr>
71 <tr><td><code>(?=...)</code></td><td>Positive lookahead</td></tr>
72 <tr><td><code>(?!...)</code></td><td>Negative lookahead</td></tr>
73 <tr><td><code>(?&lt;=...)</code></td><td>Positive lookbehind</td></tr>
74 <tr><td><code>(?&lt;!...)</code></td><td>Negative lookbehind</td></tr>
75 </tbody>
76 </table>
77 </div>
78 </details>
79 <p>
80 <details>
81 <summary>Examples</summary>
82 <div>
83 <p>Here are some example regular expressions to try:</p>
84 <table>
85 <thead>
86 <tr><th>Description</th><th>Regex</th></tr>
87 </thead>
88 <tr>
89 <td>Match one uppercase letter followed by three digits</td>
90 <td><code>[A-Z]-\d{3}</code></td>
91 </tr>
92 <tr>
93 <td>Time of day (HH:MM:SS)</td>
94 <td><code>(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])</code></td>
95 </tr>
96 <tr>
97 <td>8-bit binary numbers</td>
98 <td><code>[01]{8}</code></td>
99 </tr>
100 <tr>
101 <td>Integer from 0 to 99</td>
102 <td><code>[1-9]?\d</code></td>
103 </tr>
104 <tr>
105 <td>Integer from 0 to 255</td>
106 <td><code>25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d</code></td>
107 </tr>
108 <tr>
109 <td>Roman Numerals to 50</td>
110 <td><code>(X{,3}|XL)(I{,3}|IV|VI{,3}|IX)|L</code></td>
111 </tr>
112 <tr>
113 <td>Chemical Symbol</td>
114 <td><code>A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|S[bcegimnr]?|T[abcehilm]|U(u[bhopqst])?|V|W|Xe|Yb?|Z[nr]</code></td>
115 </tr>
116 <tr>
117 <td>IPv4 addresses in 192.168.0.0/16</td>
118 <td><code>192\.168(\.((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d))){2}</code></td>
119 </tr>
120 <tr>
121 <td>MAC address</td>
122 <td><code>[0-9A-Fa-f]{2}([:-][0-9A-Fa-f]{2}){5}</code></td>
123 </tr>
124 <tr>
125 <td>UUID</td>
126 <td><code>[0-9A-F]{8}(-[0-9A-F]{4}){3}-[0-9A-F]{12}</code></td>
127 </tr>
128 <tr>
129 <td>Original US Area codes (leading and trailing digit 2-9, middle digit 1 or 0)</td>
130 <td><code>[2-9][10][2-9]</code></td>
131 </tr>
132 </table>
133 </div>
134 </details>
135 <p>Enter a regular expression to see its matching strings.</p>
136 <div class="controls">
137 <label for="regex-input">Regex:</label>
138 <input type="text" id="regex-input" placeholder="e.g. [A-Z]-\d{3}" value="">
139 <label for="max-results">Max results:</label>
140 <input type="number" id="max-results" value="200" min="1" style="width: 60px;">
141 <button id="invert-btn" py-click="do_invert">Invert</button>
142 <button id="cancel-btn" py-click="cancel_invert" style="display: none;">Cancel</button>
143 </div>
144 <div id="status"></div>
145 <textarea id="output" readonly></textarea>
146 <p>GitHub repo for this page <a href="https://github.com/ptmcg/regex_inverter.git" target="_blank">regex-inverter</a></p>
147 <p>Powered by <a href="https://pyscript.net" target="_blank">PyScript</a> and <a href="https://pypi.org/project/pyparsing/" target="_blank">pyparsing</a></p>
148
149 <py-config>
150 packages = ["pyparsing"]
151 [[fetch]]
152 files = ["inv_regex.py"]
153 </py-config>
154
155 <script type="py">
156 from pyscript import document
157 from inv_regex import invert, count
158 import pyparsing
159 import itertools
160 import asyncio
161
162 is_cancelled = False
163
164 def cancel_invert(event):
165 global is_cancelled
166 is_cancelled = True
167
168 async def do_invert(event):
169 global is_cancelled
170 is_cancelled = False
171
172 regex = document.querySelector("#regex-input").value.strip()
173 if not regex:
174 return
175
176 try:
177 max_results = int(document.querySelector("#max-results").value)
178 except ValueError:
179 max_results = 200
180 if max_results > 100_000_000:
181 max_results = 100_000_000
182 document.querySelector("#max-results").value = 100_000_000
183
184 output_area = document.querySelector("#output")
185 status_div = document.querySelector("#status")
186 cancel_btn = document.querySelector("#cancel-btn")
187
188 output_area.value = ""
189 status_div.innerText = "Processing..."
190
191 # Use a small delay to allow status to update in the UI
192 await asyncio.sleep(0.1)
193
194 try:
195 # Get up to max_results items using an iterator
196 invert_iter = invert(regex)
197 num_shown = 0
198 while num_shown < max_results:
199 results = list(itertools.islice(invert_iter, min(max_results - num_shown, 100_000)))
200 num_shown += len(results)
201 output_area.value += "\n".join(results) + "\n"
202 if len(results) < 100_000:
203 break
204 if num_shown < max_results:
205 cancel_btn.style.display = "inline"
206 await asyncio.sleep(0)
207 if is_cancelled:
208 status_div.innerText += " (cancelled)"
209 break
210
211 if not is_cancelled:
212 # Count the remaining items in the iterator
213 remaining_count = 0
214 for i, _ in enumerate(invert_iter, 1):
215 remaining_count = i
216 if i % 100_000 == 0:
217 status_div.innerText = f"Counting matches... {num_shown + i:,} found so far"
218 await asyncio.sleep(0)
219 if is_cancelled:
220 status_div.innerText += " (cancelled)"
221 break
222
223 total_count = num_shown + remaining_count
224
225 if not is_cancelled:
226 status_div.innerText = f"Total matching strings: {total_count:,}"
227 if total_count > max_results:
228 status_div.innerText += f" (showing first {max_results:,})"
229
230 except pyparsing.ParseBaseException as pe:
231 status_div.innerText = "Error"
232 output_area.value = f"Parse Error: {pe.msg}\n{pe.explain(depth=0)}"
233 except Exception as e:
234 status_div.innerText = "Error"
235 output_area.value = f"Error: {str(e)}"
236 finally:
237 cancel_btn.style.display = "none"
238 await asyncio.sleep(0)
239
240 # Add event listener for Enter key in input box
241 def on_keypress(event):
242 if event.key == "Enter":
243 asyncio.create_task(do_invert(None))
244
245 document.querySelector("#regex-input").onkeypress = on_keypress
246 document.querySelector("#max-results").onkeypress = on_keypress
247 </script>
248 </body>
249 </html>