Skip to content

Commit fa24016

Browse files
author
github-actions
committed
Sync regex_inverter example from pyparsing
1 parent ea22046 commit fa24016

2 files changed

Lines changed: 38 additions & 22 deletions

File tree

dest/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ The Regex Inverter allows you to enter a regular expression and generate all pos
2727
- `index.html`: The web interface and PyScript configuration.
2828
- `inv_regex.py`: The core inversion logic using `pyparsing`.
2929

30+
## Run the Regex Inverter web page online
31+
32+
To run the Regex Inverter from the Github Pages server, open your browser
33+
and go to [https://ptmcg.github.io/regex_inverter/](https://ptmcg.github.io/regex_inverter/).
34+
3035
## How to Run Locally
3136

3237
To run the Regex Inverter on your own machine:

dest/index.html

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ <h4>by Paul McGuire, January 2026</h4>
109109
<td>Roman Numerals to 50</td>
110110
<td><code>(X{,3}|XL)(I{,3}|IV|VI{,3}|IX)|L</code></td>
111111
</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>
112116
<tr>
113117
<td>IPv4 addresses in 192.168.0.0/16</td>
114118
<td><code>192\.168(\.((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d))){2}</code></td>
@@ -186,32 +190,38 @@ <h4>by Paul McGuire, January 2026</h4>
186190

187191
# Use a small delay to allow status to update in the UI
188192
await asyncio.sleep(0.1)
189-
193+
190194
try:
191195
# Get up to max_results items using an iterator
192196
invert_iter = invert(regex)
193-
results = list(itertools.islice(invert_iter, max_results))
194-
num_shown = len(results)
195-
output_area.value = "\n".join(results)
196-
197-
if num_shown == max_results:
198-
cancel_btn.style.display = "inline"
199-
200-
await asyncio.sleep(0.1)
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
201224

202-
# Count the remaining items in the iterator
203-
remaining_count = 0
204-
for i, _ in enumerate(invert_iter, 1):
205-
remaining_count = i
206-
if i % 100_000 == 0:
207-
status_div.innerText = f"Counting matches... {num_shown + i:,} found so far"
208-
await asyncio.sleep(0)
209-
if is_cancelled:
210-
status_div.innerText += " (cancelled)"
211-
break
212-
213-
total_count = num_shown + remaining_count
214-
215225
if not is_cancelled:
216226
status_div.innerText = f"Total matching strings: {total_count:,}"
217227
if total_count > max_results:
@@ -225,6 +235,7 @@ <h4>by Paul McGuire, January 2026</h4>
225235
output_area.value = f"Error: {str(e)}"
226236
finally:
227237
cancel_btn.style.display = "none"
238+
await asyncio.sleep(0)
228239

229240
# Add event listener for Enter key in input box
230241
def on_keypress(event):

0 commit comments

Comments
 (0)