One of my favorite regular expression parlor tricks is this regular expression which matches collections with a prime number of elements in it.

/^(?!(.{2,})\1+$).+/

https://regex101.com/r/e42RuL/1

regex101: build, test, and debug regex

Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.

regex101
@d0nut bruh this is like hieroglyphics lol. what does this even do. i love some of the streams youve done btw
@d0nut I think that's my biggest problem with regex is, i have to learn a whole new laguage just to parse the output of another language. too lazy. or give it to me in json

@strf0x It's definitely a whole new thing to learn xD

I just happened to have a summer where I was a less productive intern that I probably should've been that spent wayyyyy too much time on the regular expression questions on stackoverflow xD

@strf0x

^ // At the start of a line
(?! // check if the following does **not** match (negative look-ahead)
( // create a capture group (group 1)
.{2,} // match 2 or more of any repeating character
) // end capture group (group 1)
\1+ // repeat matching capture group 1, one or more times
$ // match the end of the line
) // end negative look-ahead
.+ // match one or more characters

Technically it should be .{2,} at the end so we ensure we don't match a single element :)

Hope this helps! And thanks for watching my streams :)

@strf0x Oh, and to explain the general idea that it's trying to convey.

In english it says this:

"Try and find two factors that are greater than 1 that, multiplied together, match every character. If that is not possible, match the whole string."

We're basically telling the regex engine to brute force finding two factors so we can do a composite test!

@d0nut I like this explanation even more
@d0nut this is awesome man thanks. going to take me a minute to digest but appreciate it1

@strf0x My absolute pleasure! :D

I love helping people with regular expression questions. They're like a fun game to me at this point xD