5 ms·
You will learn about regular expressions in 55 minutes. To learn how to use regular expressions, in as long as it might take you, it might be better to: Read t
by shittyanalogy 13y ago
You will learn about regular expressions in 55 minutes. To learn how to use regular expressions, in as long as it might take you, it might be better to:
Read the wikipedia page which has a great description of what regular expressions are and why you would use them: https://en.wikipedia.org/wiki/Regular_expression https://en.wikipedia.org/wiki/Regular_expression
Read the JS docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FRegular_Expressions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guid...
Test your regexes: http://regexpal.com/ http://regexpal.com/
Visualize your regexes: http://www.regexper.com/ http://www.regexper.com/
Try some challenges: http://callumacrae.github.io/regex-tuesday/ http://callumacrae.github.io/regex-tuesday/
And remember, writing regular expressions can be very difficult, reasoning about your regular expressions can be even more so, defining your problem can be the most difficult of all. Think before you regex.
- zecho 13y agoThe most difficult part of regex isn't defining the problem. I'd say that one's easy. The hardest part is figuring out your regex months or years after you've written it!
- riffraff 13y agoextended regexes with comments (aka /x) should be the default and enforced behaviour in all newly designed languages. Sadly, the only one that does is perl6.
- pierrebai 13y agoOne trick I've used that saves life when designing and reviewing regex and makes them readable even for later programmers who know nothing about regex is to chunk them into smaller bits with significant names. For example, using C, file-local, pre-processor: #define MATCH_NAME_RE "[a-zA-Z]+" #define MATCH_SPACES_RE "\s+" #define CAPTURE_NUMBER_RE "(\d+\)" And then: re = QRegex( MATCH_NAME_RE MATCH_SPACES_RE CAPTURE_NUMBER_RE ); It both documents what each part does, makes it easier to debug and change them later on and makes them grokable. It only breaks down when you're doing very complex matching, but most of the time RE can be broken into smaller pieces.
- deckiedan 13y agoPython has them: a = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X)
- zyxley 13y agoCoffeescript has similar functionality. From the Coffeescript docs: OPERATOR = /// ^ ( ?: [-=]> # function | [-+*/%<>&|^!?=]= # compound assign / compare | >>>=? # zero-fill right shift | ([-+:])\1 # doubles | ([&|<>])\2=? # logic / shift | \?\. # soak access | \.{2,3} # range or splat ) /// When turned into Javascript it becomes: var OPERATOR; OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/;
- riffraff 13y agoyes, any decent PL nowadays has it, my point was that it should be the default.
- xradionut 13y agoI comment them at the time of creation and add them to my snippets file with the comment.
- maaaats 13y agoHopefully you at least wrap the regex in a function, then it's easier to reason about given the function name. Regexes randomly placed in long functions are harder to grasp.
- dbbolton 13y agoI'd recommend perlretut as well: http://perldoc.perl.org/perlretut.html http://perldoc.perl.org/perlretut.html It goes into more detail about some things that were glossed over in the OP, which may be overwhelming for a newbie but necessary for advancing.
- agumonkey 13y agoLooks an interesting set of examples. I couldn't run the first one in http://perldoc.perl.org/perlretut.html#Named-backreferences http://perldoc.perl.org/perlretut.html#Named-backreferences (need parens and = assignment it seems)
- aidos 13y agoGood list of links. I had a look through the regex tuesday list. It looks like fun for people who know their way around regexs but if you're just learning I'd recommend starting with something simpler. These look like a more reasonable starting level : http://regexone.com/ http://regexone.com/
- BuildTheRobots 13y agohttp://debuggex.com/ http://debuggex.com/ is another fantastic resource for visualising regex's.