ReRegex
A list of regex replacements, run again and again on one string until nothing changes.
Turing complete: yes · ●●○ moderate
Tested with 6 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
ReRegex, by TehFlaminTaco on GitHub, splits its code on /. The parts come in pairs, a pattern and its replacement, and the last part is the string to work on. Each pass runs every pair as Java's replaceAll. Passes go on until a whole pass leaves the string as it was, and then the string is printed. a/b/banana turns every a into b and prints bbnbnb.
Read the story
The pairs do not run in the order they are written. The reference keeps them in a Java HashMap, so they run in the order of the patterns' hash codes. A program that depends on the order has to account for that.
The machine
| Code | Does |
|---|---|
pattern/replacement/ | One rule, a Java regex and a Java replacement with $1 groups |
| last part | The string the rules work on |
#input or (?#input) | Replaced by the whole input |
#comment (?#comment) | Removed, as are newlines in the code |
\n \/ | A newline, a slash that does not split |
With an even number of parts there is no string and nothing runs. The reference is Main.java in github.com/TehFlaminTaco/ReRegex. Its #import of a library of rules is left out, and the patterns run on JavaScript's regex engine, which shares most of Java's syntax.
Example
The third swaps ab for ba three passes in a row, until every b has moved to the front.
The third swaps ab for ba three passes in a row, until every b has moved to the front. That is a sort, done by rewriting.
Patterns it teaches
- String rewriting: replace until a fixed point, as in Thue, with regexes for rules.
Further
- The reference interpreter, in Java
See also
Also in Rewriting
Write your own in Run Convert a program from ReRegex
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: reregex.
Command line
shib run reregex -e 'a/b/banana'shib manual reregexSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/reregex.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language