4 ms·
Very nice write up, and easy to read. The author does a great job explaining the concepts. I do wish he would have dove a bit deeper though: > [0-9][0-9]* (wha
by nodesocket 7y ago
Very nice write up, and easy to read. The author does a great job explaining the concepts. I do wish he would have dove a bit deeper though:
> [0-9][0-9]* (what this pattern matches is left as an exercise to the reader)
Some more real world examples to bring all the concepts home would have been great.
- rmason 7y agoI'd also like to see a part 2 and perhaps a part 3. This is the clearest explanation of regex that I've seen - and I've seen quite a few!
- noxer 7y agoThe pattern is kinda nonsense. The first part [0-9] matches one digit in the given range 0-9. The second part obviously the same but the * quantifier means the set can occur 0 or many times. The whole pattern therefore means one digit 0-9 followed by zero or many digits in the range 0-9. so "3" would match "21" would match "123456" would match "" would not match The obviously "correct" pattern for this behavior would be [0-9]+ The + means 1 or more and thus makes repeating the set obsolete.
- Rainymood 7y agoAuthor here. I actually did not know this. I used this very old book (Unix Power Tools) and after finishing the chapter on regex I didn't recall seeing a `+` operator (maybe I forgot to add it or skipped over it). But indeed the `+` operator seems to make significantly more sense here. Thanks!
- pvorb 7y agoThe "+" only saves you a few keystrokes. Using "*" is also fine and not wrong, but a little more error prone, so I would note to better use "+" in a peer review.
- noxer 7y agoExactly, it mostly about readability which is often (not always) better if the shortest possible pattern is used. [0|1|2|3|4|5|6|7|8|9][0|1|2|3|4|5|6|7|8|9]{0,} and [0-9][0-9]* and [0-9]+ and \d+ all do the same so they are not wrong as in the result will be correct.
- new_guy 7y agoGenuine question. Why would you write a guide for 'noobs', when you don't know the basics yourself? It seems kind of silly.
- Rainymood 7y agoI seem to have deleted part of the title. The article is titled >Regex For Noobs (like me!) - an illustrated guide So I am painfully aware that I'm a noob too, I even call myself one! I wrote this little piece because I felt that regex can be kind of intimidating for a lot of people but that it's actually pretty OK and fun once you get the hang of it. To get people over that initial barrier is what I aim to do with the "guide".
- aogl 7y agoBecause noobs wouldn't know to find problems ;)
- spraak 7y agoIt's a great way to reinforce your own learning, share what you just learned, and get great feedback (many of the comments in this thread)
- pvorb 7y agoPeople who are new to a topic and just learned a new thing often do a better job at explaining things without requiring prior knowledge. Experts in a topic often see things as trivial and write introductions that exclude a fraction of potential readers that lack a skill. I can recommend the articles by Julia Evans[1], which are pretty much like this. She works on a problem that is new to her and describes how she solved it or how she learned something new. [1]: https://jvns.ca/ https://jvns.ca/
- kevin_thibedeau 7y ago'+' isn't a part of BRE syntax. It stems from ERE and all of its offshoots. https://www.gnu.org/software/sed/manual/html_node/BRE-syntax.html https://www.gnu.org/software/sed/manual/html_node/BRE-syntax...
- noxer 7y agoNo problem =) I was just a bit confused myself as I saw that pattern an it didn't instantly make sense why someone would write that. If you often use regex like I do. You kinda instantly see that something is weird if pattern have repeating sub pattern. It basically should never happen if the pattern is simplified. You may wanna have a look at the PCRE flavor if you understand this you can work with all others (you'll only gonna miss some features) The + is really just a shortcut for {1,} means 1 or more greedy the * is a shortcut for {0,} means 0 or more greedy Also while I'm at it the "even more correct" pattern ofc would be \d+ (at least in PCRE) \d stand for digit it shortcut for [0-9] Personally I would never write [0-9] I only ever use sets for partial ranges i.e. [5-9] or [a-f] Whenever you need the "full" set there is usually a short way to write it. This makes pattern shorter and easier to read. Another nice trick is to uppercase the shortcuts to invert it's meaning so \D would match any non-digit.