6 ms·
Understanding Regular Expressions 2: Groups and Captures
- junto 12y agoPython and .NET also have named groups/captures. I find that particularly useful when dealing with more complex expressions. http://www.regular-expressions.info/named.html http://www.regular-expressions.info/named.html
- liquidise 12y agoVery true. I plan on writing about those in a coming post, along with zero-widths and look aheads/behinds. It has been a challenge trying to distill such a powerful tool into its logical building blocks, without biting off too much in any individual post.
- zo1 12y agoComment on a tangent. I often find people absolutely baffled, and at the same time amazed at regular expressions. I am, of course, referring to my coworkers, whom I try to convince to at least attempt at using regular expressions. Unfortunately, no matter how many cheat-sheets I print and handout, or how many times I wow them with simple yet powerful regular expressions, they resort to using the proverbial brute-force hammer that they know. Has anyone had any positive luck with convincing/converting coworkers into using more regular expressions?
- jefe78 12y agoThe most luck I had with this was tackling a massive problem with something elegant then offering the idea of regular expressions when a similar problem came up for my colleagues.
- deleted 12y ago[deleted]
- acjohnson55 12y agoRegex as a language kind of sucks. If you don't use it on a daily basis, it's pretty easy to let your understandability of it slip to the point where the average regex looks like symbol soup. At the cost of terseness, I think representing regexes as a chain of operations would be much more practical. I've seen at least one library do this on HN probably a couple years back, and it was roundly derided in the comments. But failing adoption of such a library, there's not a whole lot of excuse for a professional programmer not to be able to look up some syntax to whip up a regex in cases where it's the right tool for the job.
- leeoniya 12y agojust show them the equivalent amount of code that includes the lexer/parser and validation logic that can be summarized by even a simple regex. aversion to regex is like aversion to SQL. sometimes declarative saves literally hundreds of lines of hard-to-understand, poorly optimized code.
- liquidise 12y agoi used to try this, but young programmers especially tend to just leave thinking "regex is so powerful but i don't know it", which was likely their mindset going in anyway.
- rndmize 12y agoI acquired a dislike of regexes in school, and it wasn't until I found http://www.regexr.com http://www.regexr.com that I felt I could efficiently figure out how to use them (and started to feel that they were seriously awesome). I think that regexes, like so many programming tools/utilities, have such a frustrating initial learning period that many people don't want to bother. Regexr sidesteps this by not only having hints/cheat sheets on the side, but it shows you your results as you construct it; faster feedback = faster learning.
- wodenokoto 12y agoIf people don't want to learn it, it won't happen. With that being said, I found the introduction to regular expressions in Speech and Language Processing, by Daniel Jurafsky to be the only one where it clicked for me what was going on.
- georgiecasey 12y agoRegexBuddy is the best €30 I spent for speeding up and testing expressions. Pity it doesn't support OBjC though.