8 ms·
>Regular Expressions, abbreviated as RegEx or RegExp, are a string of characters created within the framework of RegEx syntax rules. You can easily manage your
by Zhyl 5y ago
>Regular Expressions, abbreviated as RegEx or RegExp, are a string of characters created within the framework of RegEx syntax rules. You can easily manage your data with RegEx, which uses commands like finding, matching, and editing. Regex can be used in programming languages such as Phyton, SQL, Javascript, R, Google Analytics, Google Data Studio, and throughout the coding process. Learn regex online with examples and tutorials on RegexLearn now.
I always look at these intros/descriptions of Regex with a heavy heart. They describe what regex's are, but none of the info is going to make much sense to someone who doesn't already know why they would want to learn them.
The best motivation for regexes that I've read is actually from a Python Tutorial [0] where the author gives an example of writing a lot of nested 'if' statements that could all be solved by a single regex. On the whole, I think regexes are one of the most powerful tools that doesn't have enough publicity in large part due to this Catch 22 of trying to explain what they are.
[0] https://automatetheboringstuff.com/chapter7/ https://automatetheboringstuff.com/chapter7/
- MeinBlutIstBlau 5y agoFor heavy optimizations, I've learned that looping through each char and maybe passing in an array of illegal or preferred chars is faster than Regex. It's about 2-2.5x the speed from my basic example. So if you're processing lots of stuff, manual loops might be better. If not, Regex is the way to go.
- umvi 5y agoRegex is powerful but I've found like 90% of the time I encounter one it would have been far simpler and more readable to use find + substring indexing or string splitting. Imo replacing several nested if statements with a single esoteric regex is not necessarily a win. It depends on if pattern matching is really the best tool for the job.
- Zhyl 5y agoIt very much depends what the use case is. I find that a lot of the text processing I do is easier to use back references or other regexy things. Having said this, I use tools that make regexes easy to use and readily available - I think in many programming languages the syntax means that other solutions are just as easy to devise and implement.
- umvi 5y agoIf you are a solo dev, you do you, but if you are working in a team and you are building huge regexes with back references and other bells and whistles... I would guess it's not very readable for your teammates. At least for me, when I look at such a regex I have to stare at it for minutes before grokking it.
- otoburb 5y agoI wonder if there's a metric for code reviews measuring mean-time-to-grok (MTTG).
- marginalia_nu 5y agoRegexps are fairly terse and replace a lot of code, compared to most languages they probably have an information density at somewhere between 10x-100x higher (i.e. it's not rare to replace 100 lines of code with 1 regex), so I think it's fair to expect it take longer to unpack their meaning.
- burnished 5y agoWold that be a reasonable time to ping that coworker and ask them "what does this do?". Not because you can't figure it out, but because they already know?
- wruza 5y agofind + substring indexing An endless source of off-by-one errors, not to mention buffer overflows, index out of bounds exceptions, accidental negative indexing.
- marginalia_nu 5y agoHow are you both getting buffer overflows and bounds check failures in the same code? Anyway, these are problems that arise if you don't test the code. In that scenario, regexps are an endless source of unexpected behavior as well, including in some implementations stack overflows and ReDoS attack-surfaces.
- beckman466 5y ago> I always look at these intros/descriptions of Regex with a heavy heart. They describe what regex's are, but none of the info is going to make much sense to someone who doesn't already know why they would want to learn them. > Catch 22 of trying to explain what they are. any teachers, or people who explain/document things for a living, have some good tips or templates to avoid this?
- boppo1 5y agoI don't see what's so hard. "With regex you can search for any combination of characters in a string or return any such combo or modification you like"
- da_chicken 5y agoDoesn't make sense to me. > Regular expressions (commonly known as "regex") are used for advanced pattern matching in strings. They can also be used to replace text, transform strings, or extract substrings. It's a very powerful domain-specific language that is purpose-built for string patterns and manipulation. Many general-purpose programming languages include regex engines that use similar, but often slightly different syntaxes to support the use of regex.
- quacked 5y agoYeah. 1) Encourage whoever you're teaching to stop you immediately if they don't feel like they understand something you're saying, even if it's a single word that's throwing them off, and especially if they're not rock-solid about a simple concept they "should already know". Modern school teaches people that "returning to the basics" is a waste of time; but as Feynman says, you should return to the basics often, as masters do. Pianists don't stop playing scales once they're famous. This means that if your student want to review what an "expression" is, or a what a "string" is, or what "returning" means, you've got to encourage them to do it. If a 10-minute explanation of RegEx turns into a 45-minute review of how the string variable type was invented, that will be more useful for the student in their pursuit of RegEx mastery than will a technically accurate but shallow regurgitation of your 10-minute spiel about what RegEx is. This is because they need to lay the mental framework of how they're going to think about RegEx; you are able to explain it in 10 minutes because you already have that built in your head, but they need to build those background pathways and connections themselves before analogies and summarizations make sense. 2) Try to figure out how you can make them experience the problem that led to the invention of RegEx. A student will never truly understand why a solution is valuable until they really, deeply understand the problem that the solution is solving. Note that I'm not saying that you need to teach the problem before the solution--not every student needs them in that order--just that they won't master the solution until they understand the problem. 3) In lieu of "testing" a student, have them take many breaks to re-explain what they've learned to you, even if you haven't reached a real conclusion about anything and are just checking that they understand a sentence you said. Many students, especially if they have a good teacher, will experience the sensation of comprehension even if it's not actually there. This is the "it makes sense when he says it, but when I try to explain it I can't find the words" phenomena. Taking frequent breaks to have them explain things back to you in their own words will reveal their conceptual weaknesses, and those are what you focus on. 4) Don't try to get it all done in a single session. Learning requires both forgetting and sleep. First, you should tell them to expect to forget, and that they will need to come back over and over again to topics that seem basic or simple; forgetting is part of the process of learning, like painting multiple layers on a wall. Second, they need to sleep in between sessions, which means that you can't teach everything in one day and you can't learn everything in one day, and multiple days may need to be spent reviewing the same material. This all makes a lot more sense when you treat learning like sports. Learning <programming topic> is like learning a slice serve in tennis. You don't need to serve slice, especially if you can hit flat serves at 115 mph, but serving slice is an invaluable technique when you're playing someone who can't return slice serves at all--that's a near-guaranteed 3/6 games out of every set. But in order to learn it, you need to focus on your tennis fundamentals (stay loose, eye on the ball, toss correctly), practice the same basic movements over and over again, get lots of sleep, and understand why you're learning the skill in the first place.
- dougmwne 5y agoI think regex is absolutely terrible garbage. It’s very powerful, I do appreciate not having to write complex conditional statements, and it’s great that it’s available is so many languages and applications. But it’s just a bad tool, terribly unreadable, easy to introduce bugs, with lots of trial and error. It has too much brevity and would be much better if it were longer but more human readable. I’m sure there are other string search paradigms that are far better but relatively unknown.
- jobe_br 5y agohttps://github.com/AbhinavOmprakash/luna https://github.com/AbhinavOmprakash/luna
- tyingq 5y agoYou can get a feel for what that would look like here: https://metacpan.org/release/CHROMATIC/Regexp-English-1.01/view/lib/Regexp/English.pm https://metacpan.org/release/CHROMATIC/Regexp-English-1.01/v... But then you're just memorizing things like 'start_of_line' instead of '^'. Perhaps easier to read, but no easier to write.
- cinntaile 5y agoReadability is very important though. If you can spend a couple of more seconds of programmming time to prevent several minutes(or longer!) of understanding time, I'd call that a good use of resources. I don't think that link is quite there yet but it's a good start.
- Zababa 5y agoThere's a good article about K that can give you a feel on how long names may not always be more readable: http://nsl.com/papers/denial.html http://nsl.com/papers/denial.html
- tyingq 5y agoIt's more readable individually, but for many regexes the verbose nature could make it harder to read overall.
- kaycebasques 5y ago> They describe what regex's are, but none of the info is going to make much sense to someone who doesn't already know why they would want to learn them. I frequently use the journalist's "5 Ws and H" framework as a checklist procedure for ensuring my technical communication covers fundamental questions/ideas: * Who * What * Where * Why * When * How The slightly tricky thing is that you have to formulate a question for each W based on your domain. For example what is a fruitful "where" question for RegEx? Nonetheless the checklist makes me less likely to miss very key ideas, such as "why" one would use RegEx. To make this idea more procedural maybe we could just formulate it as ungrammatical questions where you put the key topic after each W: * Who RegEx? * What RegEx? * Where RegEx? * Why RegEx? * When RegEx? * How RegEx? And then just let your mind flesh them out into more complete questions...
- chiefalchemist 5y agoThat's a helpful list / framework. Thanks. That said, where I see most tech sites / products fail is on addressing benefits. Why should I care? (As opposed to the brand or product why.) I wish I had $20 for every "Looks cool. But it's not clear to me my life will be any better."
- matheusmoreira 5y agoAlso interesting is the fact that parsers and languages are never mentioned. Regular expression engines are parsers for regular languages, one step below context-free languages. I wonder why there's no context-free language parsers in standard libraries. The Earley parser can take grammars as input without necessarily having to generate code, it would be a great algorithm for a standard context-free parser.