Discuss Scratch

gosoccerboy5
Scratcher
1000+ posts

Regex

Regex, or regular expressions, are a tool in many language that allow for fast, complex, and efficient pattern matching.

Regex implementations may vary from language to language, but they also share many things in common.

I was gonna write a tutorial on Regexes, but I don't feel like I have the time or effort.
Instead, read these links:
And testing resources:

Discuss regex here! What have you created? Do you have any questions?

Real life example:
String.prototype.toTitleCase = function() {
    return this.replaceAll(/([^\w]|^)\w/g, function(n) {return n.toUpperCase();});
};
console.log("test for the 'title-case' function..yeah :d".toTitleCase());
(Yeah.. I know I'm not good at regex or programming anything in general.. lol)
gdpr5b78aa4361827f5c2a08d700
Scratcher
1000+ posts

Regex

pain in the form of a query language.
gosoccerboy5
Scratcher
1000+ posts

Regex

potatophant wrote:

pain in the form of a query language.
I think regex is fun, but I probably haven't applied it enough to feel the true pain..
9gr
Scratcher
1000+ posts

Regex

thank you
gosoccerboy5
Scratcher
1000+ posts

Regex

9gr wrote:

thank you
for what?
Raihan142857
Scratcher
1000+ posts

Regex

here's a program using a regex to detect if a number is a prime
!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
(src)

Last edited by Raihan142857 (June 3, 2021 20:39:29)

gosoccerboy5
Scratcher
1000+ posts

Regex

Raihan142857 wrote:

here's a program using a regex to detect if a number is a factorial prime
!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
(src)
woah.. that's EPIC
Greg8128
Scratcher
500+ posts

Regex

Raihan142857 wrote:

here's a program using a regex to detect if a number is a factorial
!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
(src)
That specific program checks whether a number is prime. Prime numbers are not the same thing as factorial numbers. (the only number that is a prime and also a factorial is 2)

Last edited by Greg8128 (June 3, 2021 20:29:05)

Raihan142857
Scratcher
1000+ posts

Regex

Greg8128 wrote:

Raihan142857 wrote:

here's a program using a regex to detect if a number is a factorial
!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
(src)
That specific program checks whether a number is prime. Prime numbers are not the same thing as factorial numbers. (the only number that is a prime and also a factorial is 2)
oops, that was a typo
Greg8128
Scratcher
500+ posts

Regex

Modern RegExp is sort of an abomination btw. A simpler model of RegEx could be implemented with a finite state machine and work extremely quickly as a result. But modern RegExp has evil features such as backreferences which require the RegEx engine to use backtracking. The result is that the engine does not work efficiently for many regular expressions. For example, consider the following problem:
/^(a+)+$/.test("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")
The backtracking algorithm has to check so many possible cases that it crashes. The time required grows exponentially relative to the string length, if not faster. A simpler RegEx model could allow the engine to generate a finite state automaton which solves the problem in linear time (proportional to the length of the string)

imfh
Scratcher
1000+ posts

Regex

I learned regex from the Python docs. They are a pretty good reference, particularly if you are using regex in Python.

https://docs.python.org/3/library/re.html
Sheep_maker
Scratcher
1000+ posts

Regex

randexp.js is pretty cool for generating a random string based on a regex
gosoccerboy5
Scratcher
1000+ posts

Regex

Sheep_maker wrote:

randexp.js is pretty cool for generating a random string based on a regex
That's cool!

imfh wrote:

https://docs.python.org/3/library/re.html
That seems like a really good tutorial
linearlemur
Scratcher
500+ posts

Regex

Hey how do you guys pronounce Regex?

I pronounce it “Reejex”.
gosoccerboy5
Scratcher
1000+ posts

Regex

linearlemur wrote:

Hey how do you guys pronounce Regex?

I pronounce it “Reejex”.
I, um, pronounce it reg-ex. reg as in regular, ex as in expert.
ScratchCatHELLO
Scratcher
1000+ posts

Regex

gosoccerboy5 wrote:

linearlemur wrote:

Hey how do you guys pronounce Regex?

I pronounce it “Reejex”.
I, um, pronounce it reg-ex. reg as in regular, ex as in expert.

ɹedʒeks
gosoccerboy5
Scratcher
1000+ posts

Regex

Greg8128 wrote:

backtracking
I looked that up, but I don't really understand what it is. Could you explain it in-depth?
Greg8128
Scratcher
500+ posts

Regex

gosoccerboy5 wrote:

Greg8128 wrote:

backtracking
I looked that up, but I don't really understand what it is. Could you explain it in-depth?
The RegEx strategy is as follows:
The RegEx engine tries to match a string from start to end. But sometimes, the RegEx engine has to make a choice:
"n?"   Should the engine try to match "n", or skip it entirely?
"x*" How many times should the engine try to match "x"?
"a|b" Should the engine match "a" or "b"?
The engine needs to be able to try all possibilities. The strategy it uses is to go through the possibilities one at a time. When there is more than one choice, the engine remembers its current position. Then, it tries a choice. If it doesn't work, it goes back and tries the next choice.

The problem with this strategy is that, with certain “evil” RegExes, the amount of paths through a string increases exponentially with the length of the string. As a result the engine will never finish if it tries to match an evil RegEx against a string of moderate length.

Examples of evil RegExes (originally from Wikipedia)
(a+)+
([a-zA-Z]+)*
(a|aa)+
(a|a?)+
(.*a){x} for x > 10

A better strategy would be to transform the regex into a nondeterministic finite automaton (NFA) and run all the branches in parallel. This way, if two branches “meet”, then they can be treated as one, and so this algorithm is very efficient.

However, it does not work for Perl-Compatible Regular Expressions (PCREs) because of the existence of capture groups, which the engine must remember. This makes it impossible to make a PCRE engine using an NFA.
imfh
Scratcher
1000+ posts

Regex

Is it bad to use regex to get values from an xml file that follows a fixed format?
Greg8128
Scratcher
500+ posts

Regex

imfh wrote:

Is it bad to use regex to get values from an xml file that follows a fixed format?
Depends on what the format is and how the values need to be collected

Powered by DjangoBB