Discuss Scratch
- gosoccerboy5
-
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:
Discuss regex here! What have you created? Do you have any questions?
Real life example:
(Yeah.. I know I'm not good at regex or programming anything in general.. lol)
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:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet
- https://regex101.com/ - match, debug, and find+replace with regexes.
- https://regexr.com/ - like regex101.
- https://regexper.com/ - visualize your regexes.
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());
- gosoccerboy5
-
1000+ posts
Regex
I think regex is fun, but I probably haven't applied it enough to feel the true pain.. pain in the form of a query language.
- Raihan142857
-
1000+ posts
Regex
here's a program using a regex to detect if a number is a prime
(src)
!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
Last edited by Raihan142857 (June 3, 2021 20:39:29)
- gosoccerboy5
-
1000+ posts
Regex
factorial primewoah.. that's EPIC here's a program using a regex to detect if a number is a(src)!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
- Greg8128
-
500+ posts
Regex
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) here's a program using a regex to detect if a number is a factorial(src)!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
Last edited by Greg8128 (June 3, 2021 20:29:05)
- Raihan142857
-
1000+ posts
Regex
oops, that was a typoThat 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) here's a program using a regex to detect if a number is a factorial(src)!/^(.|(.+.)\2+)$/.test(Array(-~prompt()))
- Greg8128
-
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:
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)
/^(a+)+$/.test("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab")
- imfh
-
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
https://docs.python.org/3/library/re.html
- Sheep_maker
-
1000+ posts
Regex
randexp.js is pretty cool for generating a random string based on a regex
- gosoccerboy5
-
1000+ posts
Regex
randexp.js is pretty cool for generating a random string based on a regexThat's cool!
https://docs.python.org/3/library/re.htmlThat seems like a really good tutorial
- gosoccerboy5
-
1000+ posts
Regex
I, um, pronounce it reg-ex. reg as in regular, ex as in expert. Hey how do you guys pronounce Regex?
I pronounce it “Reejex”.
- ScratchCatHELLO
-
1000+ posts
Regex
I, um, pronounce it reg-ex. reg as in regular, ex as in expert. Hey how do you guys pronounce Regex?
I pronounce it “Reejex”.
ɹedʒeks
- gosoccerboy5
-
1000+ posts
Regex
I looked that up, but I don't really understand what it is. Could you explain it in-depth? backtracking
- Greg8128
-
500+ posts
Regex
The RegEx strategy is as follows:I looked that up, but I don't really understand what it is. Could you explain it in-depth? backtracking
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 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
-
1000+ posts
Regex
Is it bad to use regex to get values from an xml file that follows a fixed format?
- Greg8128
-
500+ posts
Regex
Depends on what the format is and how the values need to be collected Is it bad to use regex to get values from an xml file that follows a fixed format?