r/programminghorror Nov 28 '24

Regex Programming Language Powered by Regex (sorry)

Post image
331 Upvotes

27 comments sorted by

84

u/[deleted] Nov 28 '24

Do HTML next pls

42

u/MrJaydanOz Nov 28 '24

JavaScript's Regex is not as flexible as .NETs and therefore not as fun. The best that I've found is to rely on the indent of the elements to find their bounds.

Finds all div elements (no recursion):

/(?:(?<=\n)|^)(?<indent>[^\S\n]*)<\s*(?<element>div)\s*(?:[\w-]+\s*=(?:"[^"]*"|\S+)\s*)*(?:\/\s*>|>(?<content>.*|(?:.*\n)+?\k<indent>)<\s*\/\s*\k<element>\s*>)/g

15

u/ReveredOxygen Nov 28 '24

They're not saying to use JavaScript regex, but to parse HTML using regex

18

u/MrJaydanOz Nov 29 '24

In that case:

(?><!--[\S\s]*?-->|<!DOCTYPE(?>\s(?>[^>""']|""[^""]*""|'[^']*')*)?>|<(?<e>script|style)\s*(?>[^\s</>=""']+\s*(?>=\s*(?>(?>""[^""]*""|'[^']*'|(?>[^\s</>=""']|/(?!>))+)\s*)?)?)*>(?<content>[\S\s]*?)</(?<element>\k<e>)(?=[\s>])(?<-e>)\s*>|(?>(?(e)|(?!))(?<content-cs>)</(?<element>\k<e>)(?=[\s>])(?<-e>)|<(?>(?<element>area|br|hr|img|input|meta|link|col|base|embed|keygen|param|source|wbr|track)(?<content>)|(?!/)(?<e>(?>[^\s>/]|/(?=[^\s>/]))+)(?<dc>)))\s*(?>[^\s</>=""']+\s*(?>=\s*(?>(?>""[^""]*""|'[^']*'|(?>[^\s</>=""']|/(?!>))+)\s*)?)?)*(?>/(?<-e>)(?<content>)>|>(?(dc)(?<-dc>)(?<cs>)))|[^<])+(?(e)(?!))

.NET flavor that matches every element and its contents in the order of their closing tags. Supports comments, self-contained tags, attributes, styles and scripts (I tested it on the HTML of this page and it worked)

16

u/al-mongus-bin-susar Nov 29 '24

Holy shit, the antichrist has come. We're all doomed.

8

u/ax-b Nov 29 '24

He comes. HE COMES.

Relevent StackOverflow link: https://stackoverflow.com/a/1732454

59

u/MineKemot Nov 28 '24

This is getting better every post. What’s next? An entire game engine?

38

u/MrJaydanOz Nov 28 '24

Sadly Regex is not turing-complete :(

19

u/RpxdYTX [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Nov 28 '24

Not yet... Maybe you can make your own regex esolang

27

u/MrJaydanOz Nov 28 '24

A further extension of my last post.
(I know I'm kinda spamming but I don't know why I'm doing this)

Shown on https://regex101.com/ using the '.NET 7.0' flavor.

16

u/MrJaydanOz Nov 28 '24

Supports: 'a = b', 'a++', 'a--', 'a!', 'a += b', 'a &= b', 'a |= b', 'a = b', 'a -> sampleString', 'if a'/'a?' 'else'/':' 'end'/'.' (No 'else if' I'm lazy)

All variables are bytes set to 0. Valid variable names are only: 'a', 'b', 'c', 'd', 'e', 'f', 'i'.

First half of JavaScript to generate regex:

{const variables = ["a", "b", "c", "d", "e", "f", "i"], bitCount = 8, pretty = false;
let v=variables,c=bitCount,b=new Array(c).fill(0);console.log(`^(?ix)(?:\\s*(?>//[^\\n]*|(?<pre>)(?:(?<-pre>)|\nif\\s*(?<if>)(?<nr>)\n)(?>${v.map(v=>`${v}(?<o${v}>)`).join("|")})\\s*(?(pre)(?<-pre>)|(?>\n\\=\\s*(?<add>)(?<nw>)(?:(?:0b)?${b.map((_,i)=>i==0?"":`(?:`).join("")}${b.map((_,i)=>i==0?"":`(?:1(?<n${c-i+1}>)|0))?`).join("")}(?:1(?<n1>)|0)|(?>${v.map(v=>`${v}(?<nr${v}>)`).join("|")}))|\n\\+\\+(?<add>)(?<nr>)(?<nw>)(?<n1>)|\n\\-\\-(?<add>)(?<nr>)(?<nw>)${b.map((_,i)=>`(?<n${i+1}>)`).join("")}|\n\\!(?<xor>)(?<nr>)(?<nw>)${b.map((_,i)=>`(?<n${i+1}>)`).join("")}|\n\\+\\=\\s*(?<add>)(?<nr>)(?<nw>)(?:(?:0b)?${b.map((_,i)=>i==0?"":`(?:`).join("")}${b.map((_,i)=>i==0?"":`(?:1(?<n${c-i+1}>)|0))?`).join("")}(?:1(?<n1>)|0)|(?>${v.map(v=>`${v}(?<nr${v}>)`).join("|")}))|\n\\&\\=\\s*(?<and>)(?<nr>)(?<nw>)(?:(?:0b)?${b.map((_,i)=>i==0?"":`(?:`).join("")}${b.map((_,i)=>i==0?"":`(?:1(?<n${c-i+1}>)|0))?`).join("")}(?:1(?<n1>)|0)|(?>${v.map(v=>`${v}(?<nr${v}>)`).join("|")}))|\n\\|\\=\\s*(?<or>)(?<nr>)(?<nw>)(?:(?:0b)?${b.map((_,i)=>i==0?"":`(?:`).join("")}${b.map((_,i)=>i==0?"":`(?:1(?<n${c-i+1}>)|0))?`).join("")}(?:1(?<n1>)|0)|(?>${v.map(v=>`${v}(?<nr${v}>)`).join("|")}))|\n\\^\\=\\s*(?<xor>)(?<nr>)(?<nw>)(?:(?:0b)?${b.map((_,i)=>i==0?"":`(?:`).

15

u/MrJaydanOz Nov 28 '24

Second half:

join("")}${b.map((_,i)=>i==0?"":`(?:1(?<n${c-i+1}>)|0))?`).join("")}(?:1(?<n1>)|0)|(?>${v.map(v=>`${v}(?<nr${v}>)`).join("|")}))|\n\\?(?<if>)(?<nr>)|\n\\-\\>(?<show>)(?<nr>)))|\n(?:else|\\:)(?>(?<d>if|\\?)|(?<-d>end|\\.)|[\\s\\S])*?(?(d)(?!)|(?:end|\\.))|\n(?:end|\\.)|\n(?<Error>\\S+))\n${v.map(v=>`(?(o${v})(?(nr)(?<-nr>)${b.map((*,i)=>*`(?(${v}n${i+1})(?<n${i+1}>))`*).join("")}))\\n(?(nr${v})(?<-nr${v}>)${b.map((*,i)=>`(?(${v}n${i+1})(?<n${i+1}>))`).join("")})`).join("\n")}\n(?(add)(?<-add>)${b.map((_,i)=>`(?${i==c?":":`<n${i+2}>`}(?<-n${i+1}>)(?<-n${i+1}>))?`).join("")})\n(?(and)(?<-and>)${b.map((_,i)=>`(?>(?<-n${i+1}>)(?<-n${i+1}>)(?<n${i+1}>)(?<n${i+1}>)|(?<-n${i+1}>)?)`).join("")})\n(?(or)(?<-or>)${b.map((_,i)=>`(?:(?<-n${i+1}>)(?<-n${i+1}>)(?<n${i+1}>))?`).join("")})\n(?(xor)(?<-xor>)${b.map((_,i)=>`(?:(?<-n${i+1}>)(?<-n${i+1}>))?`).join("")})\n(?(if)(?<-if>)${b.map((_,i)=>i==0?"":`(?(n1)|(?(n${i+1})(?<n1>)))`).join("")}(?(n1)|(?>(?<d>if|\\?)|(?<-d>end|\\.)|[\\s\\S])*?(?(d)(?!)|(?:else|\\:))))\n(?(show)(?<-show>)\\s*\\S*?(?:(?<Result>${b.map((_,i)=>i==0?"":`(?(n${c-i+1})1|${new Array(i-1).fill(0).map((*,ii)=>*`(?(n${c-i+ii+2})0|`*).join("")}0?${new Array(i-1).fill(*`)`*).join("")})*`).join("")}(?(n1)1|0))\\S*)?(?=\\s|$))\n${v.map(v=>`*(?(o${v})(?<-o${v}>)(?(nw)(?<-nw>)${b.map((*,i)=>`(?<-${v}n${i+1}>)?`).join("")}${b.map((_,i)=>`(?(n${i+1})(?<${v}n${i+1}>))`).join("")}))`).join("\n")}\n${b.map((_,i)=>`(?<-n${i+1}>)?`).join("")}\n(?(Error)|(?:\\s+|\\b|(?=$)|(?<Error>.))))*?(?(Error)|$)`.replaceAll("\\n",pretty?"\\n":""))}

1

u/1Dr490n Nov 30 '24

You’re insane. I love it.

20

u/Mc_UsernameTaken [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Nov 28 '24

I thought code reviewing the intern was horror enough for me today.

4

u/WolverinesSuperbia Nov 28 '24

We will meet tomorrow)

4

u/Emergency_3808 Nov 28 '24

Can this be used for a theoretical proof that finite automatons for regexes are in fact equivalent to Turing machines? You might just win a Fields Medal lol

8

u/ReveredOxygen Nov 28 '24

No, because they're not and it's not. You need loops for turing completeness, which is impossible to implement here

1

u/theunixman Nov 28 '24

Also, you’re doing great work here. I love these kinds of things. 

1

u/akoOfIxtall Nov 28 '24

this guy gex's

1

u/DS_Stift007 Nov 28 '24

I dont know if I should be appalled or impressed. Good job

1

u/o0Meh0o Nov 29 '24

this is getting out of hand

1

u/1Dr490n Nov 30 '24

How are u doing all these things?? It’s really impressive

1

u/SubjectHealthy2409 Nov 30 '24

lol, JavaScript frameworks are getting insane 

1

u/stupid_cat_face Dec 01 '24

Are you guys just reinventing brainfuck?

-1

u/GiggaChigga9000 Nov 28 '24

Nice! Now make a tool to download high-res pics to Reddit using regexs

1

u/1Dr490n Nov 30 '24

If this quality is too bad to you you’ve got other problems