12 ms·
Learn RegEx step by step, from zero to advanced
- tomhunters 5y agoNow, this is the time to learn Regex the proper way.
- Mockapapella 5y agoRegex is one of those things that I don't use often enough to really remember the syntax, so every time I end up needing it I have to relearn it from the ground up.
- greenail 5y agoI found logstash's Grok a good tool because it is composable and you can reuse complex expressions with simpler naming. There is also a knowledge base of good regex you can use without reinventing the wheel. Rubular.com is also a great tool for working out regex matches.
- WhitneyLand 5y agoAt first glance this looks like software that’s a bit dated and nothing special, but try to use it just once and tell me if you don’t agree it’s freaking awesome when do you want to create an expression or get a feel for exactly how it’s working. I have no affiliation with these guys, I’ve just been impressed over the years https://regexbuddy.com https://regexbuddy.com
- yigitkonur35 5y agoI like the way you teach them by an interactive training. I have only spent few minutes and really understand why I have copied and pasted that thing from Stackoverflow. If you start with a challenge from a complex log file, it makes it easy to learn by providing extra motivation.
- OpFour 5y agostarted by adding "ok" in the first lesson... lock just shakes and when clicking the answer, it shows the exact same thing thats already in the box :/
- shoeffner 5y agoYou need to press enter/return to advance.
- tyingq 5y agoActually, it needs to be uppercase "OK". I typed "ok" also, and scratched my head for a while. I get it should be obvious, but my brain just didn't get it right away.
- mjmsmith 5y agoReally doesn't help that the capitalized "OK" in "Start by typing OK in the RegEx field" is in a smaller font.
- account-5 5y agoI have learned regex, I learned it from regexone years ago so appreciate this type of teaching method. What I will say though it it's a little pedantic if you won't let me move on despite my solution being correct. An example is this: [dbf]eer When the answer required was (if I remember correctly): [bdf]eer I think as long as you're correct you should be allowed to move on. By all means show your preferred solution.
- somedude895 5y agoWhat's also a bit of a pain is when you click Show Solution you still have to type it in the box to continue. Apart from that I love it! I normally use regexr.com and just mess around with it until I get the desired result. It also helps with learning, but you end up never truly understanding the concepts.
- leokennis 5y agoFor most purposes, sites like that or https://regex101.com/ https://regex101.com/ are fine. But, small differences in regexes (greediness, backtracking) can make huge performance differences that you won't notice when testing it on 2 or 3 lines on a site like that. A nice example was a big Cloudflare outage in 2019: https://blog.cloudflare.com/details-of-the-cloudflare-outage-on-july-2-2019/ https://blog.cloudflare.com/details-of-the-cloudflare-outage... So for anyone using regex in (1) production as (2) part of an automation / regular process (i.e. not a one time search) on (3) sizeable amounts or reoccurrences of data, I'd really advice gaining a deeper understanding of what the various options do.
- asicsp 5y agoSite looks cool. Good to see you've made it interactive. Seems like you are supporting JS flavor, that should be mentioned prominently. Suggestions for the cheatsheet: 1) Multiline example regex is missing the anchors 2) Negative lookbehind should be `(?<!)` not `(?!)` 3) `+` and `*` examples are using `()` around a character, which isn't needed (the `?` example doesn't use it)
- bevacqua 5y agohighly recommend Mastering Regular Expressions
- CyberMew 5y agoAre you referring to a book or online resource?
- davidpolberger 5y agoHe or she is probably referring to Jeffrey Friedl's classic tome: http://regex.info/book.html http://regex.info/book.html
- laputan_machine 5y agoThis looks nice but is telling you what to do, instead of giving you problems to solve. I don't think this is a good method of teaching. Edit: After lesson 8 you start to get problems to solve Edit 2: I take it back, this is the first time I've understood how lookarounds work. Great stuff!
- Cthulhu_ 5y agoThat's what it starts out, then it tests you with some problems to solve based on what you learned before.
- 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.
- brainwipe 5y agoI learn regex just before I need it. Every. Time. After 25 years, it just doesn't stick.
- city41 5y agoI'm the same way. I suppose some people use regex's a lot, I never really have. If our experiences are typical, I'd argue people new to regex's should more learn they exist and when they can be useful, and not worry too much about actually learning their mechanics.
- boppo1 5y agoThis was my approach and I found it pretty successful the first and most recent time I used regex.
- soco 5y agoSame here. Because I never need "regex" as a skill by itself in a project, but just needed to search/replace one damn string for one particular situation, and next year another situation in a completely unrelated place for completely unrelated reasons so whatever I learned (and forgotten) last year wouldn't really apply anyway.
- jmnicolas 5y agoWhy use it though? I'd rather wright or read 30 lines of code instead of trying to decipher what a regex means or do and making sure it is covering all possible cases. I don't understand the goodwill toward regexes. It's basically an embedded BrainFuck in your programming language. I'm 14 years in my dev career, there never was a moment where not using regexes came to be a problem.
- boppo1 5y agoI think it's case-by case. I once scraped a list of names and dates from individual Wikipedia pages. There were lots of formats like "1900-1950", "1900 - 1950", "(1900 - 1950)", "(1900 to 1950)", "1900 to cf. 1950", and so on. These were arbitrarily nested in the first couple sentences. My thought was "Oh I think this is a job for that regex thing" and 35 minutes of googling syntax + a handful of passes later I had all the dates in a workable table. I have no idea how much code that would have taken. Albeit, I am a novice programmer.
- badtension 5y agoSome exercises are formulated in a weird way. It takes a while to understand what they want from me. > Write the expression using curly brackets {} to select the numbers from 0 to 9 in the text that is at least between 1 and 4.
- deleted 5y ago[deleted]
- diogenesjunior 5y agoThank you for this. I have been trying to learn regex for a while, but this makes it easier.
- nightbrawler 5y ago[0] Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. I love regular expressions and figuring out the syntax to solve problems I encounter where I can use regex. Such as searching code for calls with specific named params that may be in different order across the code base. But I always have this quote in the back of my mind if I'm thinking about using regex in code, especially areas that get hit a lot. [0] https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ https://blog.codinghorror.com/regular-expressions-now-you-ha...
- TheCapn 5y agoI once asked my friend: "Is there anything Regex can't do?" He replied: "Work the first time?" And that's how I always think of Regex now. It's an incredibly powerful tool that I pull out pretty often, but if I'm doing anything beyond the most basic pattern matching I know I'm about to lose a bit of time questioning my abilities and sanity.
- laputan_machine 5y agoIt's really subject-specific. What I usually see is a small problem easily solved by Regex, that a few months later down the line we need to expand the regex a little bit, but that's ok it's still readable... repeat this a bunch of times and now you have a critical regex line that nobody really understands how it works and nobody ever wants to touch without it being unit-tested to death. It's just a riff on tech-debt, imho
- Yajirobe 5y agoWe need a natural-language <-> regex translator
- nightbrawler 5y agoBack when I used Windows, I had an app called Regex Buddy and you could paste in any regex and it would generate a comment block that broke down what was going on. Was super useful when you come back a few months later and need to understand a complex regex
- asdf_snar 5y agoI find it hard to appreciate the need for this website. First, after scrolling down to the bottom of the page, I still couldn't see where I could "start". I clicked the only prominent button featured on the page, "Product Hunt", and that took me to a clearly unrelated website. You'd think the green words "learn, practice, test, share" would be links, but nope. So after 30+ seconds of browsing it still isn't clear to me where the product was. I gave up. Compare that with https://regexone.com/ https://regexone.com/ . No fluff. I immediately know what I'm looking at and am given a simple practice problem, with a place to type smack in the middle of the page, and a responsive green coloring when I get something right. The problems advance to exactly the kind of stuff I need, with a quick view on all the lessons on the right-hand side. I'm visiting that website for the n^th time because I need to quickly refresh my regex to accomplish a task.
- hLsvZHKd 5y agoThe website is broken when https://www.googletagmanager.com/ https://www.googletagmanager.com/ is blocked (on any decent uBlock Origin setup). The webmaster might want to fix this ASAP, I don't know why this kind of script is "mandatory" in modern web design...
- marttt 5y agoGreat discussion. I stumbled upon a comprehensive tutorial from 2014 just yesterday: "Learn regex in about 55 minutes": https://qntm.org/re_en https://qntm.org/re_en Discussed on HN as well (78 comments): https://news.ycombinator.com/item?id=7370622 https://news.ycombinator.com/item?id=7370622
- balabaster 5y agoI've had RegexBuddy installed on my computer for so long. It's been part of my basic dev setup for years: https://www.regexbuddy.com/ https://www.regexbuddy.com/ I used to use it a lot, although I've not used it in quite a while. I don't recall the last time I even needed to write a regex. Somehow it's still stuck in my head. Well worth having it if you want a tool to hack around large blocks of text and play with regex in a "live" environment.
- nerdponx 5y ago$40 is a bit steep, but I'd probably be willing to pay for a Linux version.
- xwowsersx 5y agoI like this, but the writing is in desperate need of improvement. > The basic matcher is to type as is to choose a character or word. For example, to select the word curious in the text, type in the same way.
- avgcorrection 5y agoI respect regex for being a widespread DSL that makes certain things very declarative. My problem with it (other than all the different flavors—that’s just history’s fault) is that you don’t tend to get integrated help/tool support for them. They are often just plain strings—how is e.g. Intellij supposed to spot them reliably and help you? It’s too bad if we have to copy and paste regex strings into websites in order to figure out what they do. The number one most useful tool support in my book would just be to highlight metacharacters. Are parens a metacharacter in the dialect that I am using now? Is `+` significant? A bigger ask would be more verbose regular expression declarations and compilers that can translate to and from them. It’s nice if you can use comments in a regex dialect, but comments should be treated like they are treated in code writ large; don’t use them if you can make the code “self-describing”. Imagine a more verbose declaration language where you can make local aliases, for example `let ident = [a-z][a-z1-9_]*`.
- nerdponx 5y agoJulia has a "readable regex" library that uses descriptive English names. It's a lot like a SQL query builder, but it builds regex. https://discourse.julialang.org/t/ann-readableregex-jl/43450 https://discourse.julialang.org/t/ann-readableregex-jl/43450 https://github.com/jkrumbiegel/ReadableRegex.jl https://github.com/jkrumbiegel/ReadableRegex.jl
- hyperupcall 5y agoI agree that it would be nice to get native syntax highlighting for strings, especially for languages where regular expressions are not a first class feature However, I would realllyyy not want to have a more verbose regex dialect. For me at least, it would make recognizing common regex patterns a lot harder since they won't be succinct. It would also be yet _another_ regex variety that I would have to remember. For me, Perl's DEFINE feature and 'x' switch are definitely close enough[1] to variables and comments. Pardon, I don't remember what they are officially called. What we really need is for more non PCRE engines to implement these features [1]: https://regex101.com/r/z5pGvZ/6 https://regex101.com/r/z5pGvZ/6
- abdusco 5y ago> They are often just plain strings—how is e.g. Intellij supposed to spot them reliably and help you? WebStorm or any IDE that has WebStorm functionality built-in can detect `/pattern/` in Javascript and give you an option to test it and warn you of syntax errors. PyCharm will help you with functions in `re` module, Rider with `Regex.IsMatch` and so on, all JetBrains IDEs have some sort of contextual help that gives you guidance on stdlib functions that accept regular expressions. https://www.jetbrains.com/webstorm/guide/tips/check-regex/ https://www.jetbrains.com/webstorm/guide/tips/check-regex/
- mynameisash 5y agoI don't remember why, but I learned regexes maybe my sophomore year of college because it seemed like such an interesting thing that I might need some day? I also started to learn SQL about the same time. Those feel like herculean efforts so foreign now because I've been using them for so long. One day at my second job out of college, I wrote a quick regex for something, and one of my more senior colleagues looked at me like I was a wizard. It was amazing to be able to get some street cred for that. To me, it validated the effort I went through previously. I'm trying to learn about SAT/SMT solvers now. Not because I have a pressing need for them but because it's a completely foreign thing that - who knows? - maybe I'll be able to put to good use today. The problem with SAT/SMT is that there are nowhere near the clear learning resources compared with, say, regexes.
- dennis714 5y agohttps://sat-smt.codes/SAT_SMT_by_example.pdf https://sat-smt.codes/SAT_SMT_by_example.pdf
- chiefalchemist 5y agoMy biggest challenge with RegEx isn't the tool per se, it's the frequency I need to use it. If it were more often I'm sure more would stick. But it's such a specialized tool and the need for the speciality is rare-ish. At least for me.
- frankfrankfrank 5y agoThe english copy of some of the learning examples is rough and complex, which takes away from the lessons. An example from 21/55: "To express at least a certain number of occurrences of a character, we write the end of the character at least how many times we want it to occur, with a comma , at the end, and inside curly braces {n, }. For example, indicate that the following letter e can occur at least 3 time." It should read something like: "To express a match of a minimum number of character occurrences, we use a comma after the number of occurrences, within the curly braces {n, }. In the following, try building a regex to match the letter e occurring a minimum of 3 times sequentially."
- alphabet9000 5y agocool site and idea, here's 3 site UI suggestions there's a bug when entering text into the regex area which creates new padded elements show up which makes text on the page shift, e.g. https://i.imgur.com/AFBccIn.gif https://i.imgur.com/AFBccIn.gif the line height in the subtitle area could be increased so the text isn't so cramped ( https://i.imgur.com/HReADcS.png https://i.imgur.com/HReADcS.png ) the background color of the entire page should be darker (or use some other solution) to make the <code> looking text more distinct as code text. ( dark grey on dark blue doesn't really stand out https://i.imgur.com/8Nt9YNl.png https://i.imgur.com/8Nt9YNl.png )
- mattlondon 5y agoIt would also be nice to not have to scroll the page to click the next and previous links on each learning step (pixel 5, msedge browser). 100vh maybe?
- jgwil2 5y agoThe only way I was able to really feel like I understood regular expressions was implementing a regex engine myself.
- krisrm 5y agoRegular expressions are one of the first things I remember being taught in my intro CS class that really broadened my perspective on programming. Probably helped that the main language being taught was Perl, and my previous programming experience was BASIC ... Expectations might have been set low. One of those tools that's often over-applied, but invaluable in so many other situations.
- errcorrectcode 5y agoWhat made it stick for me: a compiler course. NFAs, DFAs, lexers, parsers, semantic rule checking, etc. Btw: Lexer-less derivation parsers are interesting because of how simple they are to write and maintain. IIRCBIMW, they are neither LL or LR.
- pkaye 5y agoIn terms of syntax, there are quite a few regex builder websites which can help you create and validate a regex pattern for a particular syntax.
- deleted 5y ago[deleted]
- dennis714 5y agoTo my taste, there are no problems with regular expressions at all. But there are problems in all sorts of escaping. Without peeking into Stack Overflow, can you tell, how to escape properly in grep/sed? If it is running in bash/zsh? In cygwin? In your favorite text editor? In bash/zsh script? How many slashes would you need for an opening bracket? For a pipe? You just never know. This frustrates heavily. I don't even try to use sed or grep. Instead, I just write a small Python or Racket script to do the job. This is what I recommend. While learning Regular Expressions, just stick to your favorite programming language with RE support. Like Python or Perl. Use it for everything you need. Then abide to a heavy burden of escaping in all sorts of exotic environments.