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+$).+/
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+$).+/
@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
^ // 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!