5 ms·
Alternatives to Regular Expressions
- gwu78 11y agoI go from BRE to flex to spitbol depending on the situation. Never had a need for PCRE. Snobol can match anything. k/q also has pattern matching. Personally, I get more mileage out of BRE than anything else. Simple and effective.
- wyager 11y agoPerl-compatible regular expressions are not semantically equivalent to real regular expressions (as the article seems to claim). In fact, they correspond to completely different grammars in the Chomsky hierarchy. PCREs are Turing complete (and for closely related reasons, extremely slow for some expressions), while regular expressions are isomorphic to FSMs (which are always linear time in the length of their input).
- alfiedotwtf 11y agoI'd hate to be writing a grammar every time I want to do a substring match. Regular expressions are convenient, and far more compact.
- adestefan 11y agoIf you're doing a substring match, then use a substring function.
- tofof 11y agoI stopped reading at: "Besides, grammars are more complex than regular expressions, so they're simpler."
- NaNaN 11y agoYou are right. The article introduce those ? + * in the grammars again, though grammers are actually simpler from some aspects.
- Retra 11y ago"I am not much of a fan of RegularExpressions. It is too hard to remember what the symbols stand for, for one. Asterisk for 0 or more repetition, plus for 1 or more repetitions, question mark for 0 or 1 occurrences, brackets surrounding a set of characters -- who's got time to memorize this kind of complexity? " Somebody please tell me this is sarcasm...
- jedmeyers 11y ago= for equals, or assign, or compare, < for less, > for more, + for adding, - for subtracting -- who's got time to memorize this kind of complexity?
- a1369209993 11y agoTechnically, compare is ==. = means "trash the left argument, then interpret the the right argument as a boolean". :)
- jqm 11y agoHis examples were bad (asterisk etc.) but the point is valid. There is some hairy regular expression syntax that is pretty difficult to grok right off the bat. It could be simpler and clearer.
- Retra 11y agoYou could say that about any syntax. But actually coming up with a better syntax is much harder than just saying it can be done.
- jqm 11y agoYou could say that about any syntax. But in most cases it would still apply much more to things like Perl regular expressions.
- vezzy-fnord 11y agoThere's VerbalExpressions, which simply wrap common PCRE tokens into methods/functions: https://github.com/VerbalExpressions https://github.com/VerbalExpressions
- bobbylox 11y agoOne alternative in the Wolfram Language is called StringExpresions. For instance, something like StringReplace["Mad Hatter", "M"|"H"~~a_~~b_..:>"B"<>a<>"g"] returns "Bag Bager" The nice thing is that a string expression can use a RegularExpression as part of the pattern, but it's often not neccesary.
- DannyBee 11y ago"The nice thing" Nothing in this post is a nice thing :)
- bane 11y ago> Maybe some use of XML No. If you're even thinking about defining a language in XML you've probably already screwed up some place.
- kidsil 11y agoI never understood why so many developers have problems with Regex. In my experience, it's difficult because most people avoid it, once you use it enough times the logic makes perfect sense.
- nicwest 11y agoit's a transferable skill as well. The engine/syntax might differ a little language to language, but the core concepts remain the same.
- roel_v 11y agoI think it's because few take the time to actually understand them before using them, and usually suffice with cobbling together a few expressions from 'tutorials' of varying quality. Reading the Friedl book at least once is essential. Then again, what do we expect when our field is filled with people whose 'education' consists of a 6-week 'hacker bootcamp' and a few weekends of watching Youtube videos... (also, too many kids on my lawn etc)
- effie 11y agoThat's a patronizing comment. Often the very education may a stumbling block in the process of learning. Most users of regexps or any other device do not need to read a thick theory book on it. Cobbling together expressions from online tutorials is a perfectly good way to learn effectively. Admittedly, it is not the best way to write production code, but there are often economic considerations that override the expert's desire for perfect design and implementation.
- roel_v 11y ago"Often the very education may a stumbling block in the process of learning." People don't want to learn because it's hard? "Most users of regexps or any other device do not need to read a thick theory book on it." Which is why they should at the very least read Friedl. "Cobbling together expressions from online tutorials is a perfectly good way to learn effectively." No it's not, otherwise we wouldn't have so many bad regexes and people asking silly questions about them, would we? "Admittedly, it is not the best way to write production code, but there are often economic considerations that override the expert's desire for perfect design and implementation." Nobody's talking about 'perfect design and implementation', just 'not be a moron' level. Because car analogies are everbody's favorite, let me use one here: no-one is saying that one should have a Formula 1 licence before driving (using regular expressions); just to have more than 20% vision in both eyes and not to drive after drinking 5 beers while texting your wife that you're on your way. Which is the car-driving equivalent of the majority of regex uses in the wild.
- kidsil 11y agoI never understood why so many developers have problems with Regex. In my experience, it's difficult because most people avoid it, once you use it enough times the logic makes perfect sense.
- kazinator 11y agohttp://www.nongnu.org/txr http://www.nongnu.org/txr
- orangeduck 11y agoLua has an alternative to Regex which is an extension of the C patterns (used in printf etc). http://lua-users.org/wiki/PatternsTutorial http://lua-users.org/wiki/PatternsTutorial At first I found it a bit confusion but they're actually pretty great. For me at least 90% of the tasks I want to do with regex can be done with scanf, and Lua's little extension covers the remaining 10% quite well. I know lots of people often say that it would be nicer to use functional composition for regex instead of strings because strings are too confusing, but I disagree with this. The confusion of regex to me is not from the string representation, it is that some characters are "special" while others are "normal" (including whitespace). At first it appears that most characters are "normal" and so you can start from some example and generalize the string until it matches all the things you want - but once you start putting parenthesis and such in you start to realize that most of the string wont be matched "normally" and it is better to start thinking like a grammar and write it from scratch. This double thinking is pretty annoying. For this reason, to me the Lua patterns are really the only alternative I've come across to regex that I've liked. They've got nice compact expressive syntax, can really easily do most of the matching tasks I need due to the scanf base, (almost) all the "special" characters begin with %, and the complex cases can still be matched.
- justincormack 11y agoOpenBSD's new web server has just started using these [1] There is still some backtracking behaviour that means you can create expressions that are slow, but for most use cases it is not an issue. [1] http://marc.info/?l=openbsd-tech&m=143480475721221&w=2 http://marc.info/?l=openbsd-tech&m=143480475721221&w=2
- hamstergene 11y agoI read the tutorial you linked and I see no difference between Lua patterns and regexes, except that '\' has been replaced with '%', and '*?' with '-'. The only thing in common with scanf is adopting '%' as control character, otherwise that's the same old regular expressions: '%d' matches just one digit, not whole signed integer like in scanf.
- draegtun 11y agoUnfortunately no examples of Rebol's parse were given. Here are some links to fill the gap: http://www.rebol.net/wiki/Common_Parse_Patterns http://www.rebol.net/wiki/Common_Parse_Patterns https://en.wikibooks.org/wiki/REBOL_Programming/Language_Features/Parse/Parsing_examples https://en.wikibooks.org/wiki/REBOL_Programming/Language_Fea... http://blog.hostilefork.com/why-rebol-red-parse-cool/ http://blog.hostilefork.com/why-rebol-red-parse-cool/ http://rebol-land.blogspot.co.uk/2013/03/rebols-answer-to-regex-parse-and-rebol.html http://rebol-land.blogspot.co.uk/2013/03/rebols-answer-to-re... http://www.rebol.com/r3/docs/concepts/parsing-summary.html http://www.rebol.com/r3/docs/concepts/parsing-summary.html http://www.rebol.net/wiki/Parse_Project http://www.rebol.net/wiki/Parse_Project
- avera 11y agoI have my regex alternative version, currently in development, with C like syntax. Here is approximate sketch, how it looks. Goal is, that from this syntax I can get full, hierarchical AST for programming languages parsing: http://pastebin.com/CFZf306p http://pastebin.com/CFZf306p - until it's done, some smaller details can change. Idea is that it can build multiple layers of matched items. In this example, layer 0 is "pattern lines" then on top of this, gets mapped tokens representation at "parttern tokens". and lastly, over lines and tokens goes AST objects, which are represented by language code. Hierarchy and context-based patterns referencing and construction can be easily achieved. Ideally, I would like to build this with realtime update feature, when used in source code editor. In IDE, when I write some characters in code, these changes propagate into parsed layers objects and updates what is needed.
- MrPatan 11y agoTo everybody hating on regexes: remember that the alternative to even a simple regex is typically a poorly-implemented ad-hoc state machine dozens of lines long full of nested loops and conditionals. Without gotos if you're lucky. _That_ is a bug breeding ground. Now, if what you are after are regular expressions with a different syntax.... well, maybe you're onto something here. But I would still call it a regex, personally.
- sklogic 11y agoNo, an alternative to the write-only regexps is nice, clean, dense but yet readable BNF or PEG syntax. And no, they're not regular expressions with a different syntax, they're much higher up in the Chomsky hierarchy.
- dragonwriter 11y ago> To everybody hating on regexes: remember that the alternative to even a simple regex is typically a poorly-implemented ad-hoc state machine dozens of lines long full of nested loops and conditionals. That's an alternative. Its not the only alternative. Monadic Parser Combinators are a thing, after all.
- jdeisenberg 11y agoPerl 6's regular expressions and grammars appear to be fairly powerful and useful. http://doc.perl6.org/language/regexes http://doc.perl6.org/language/regexes http://doc.perl6.org/language/grammars http://doc.perl6.org/language/grammars
- sklogic 11y agoI'm almost always using PEG where regexps are typically applied.
- raiph 11y agoPerl 6 unifies "regexes", PEGs, and lexical closures.[1] [1] https://en.wikipedia.org/wiki/Perl_6_rules https://en.wikipedia.org/wiki/Perl_6_rules
- thekaleb 11y agoProblems arise with regular expressions when they are used against irregular grammars.
- reirob 11y agoWouldn't Marpa [1] be an alternative to REs? [1]: http://marpa-guide.github.io/chapter1.html http://marpa-guide.github.io/chapter1.html
- plorg 11y agoI rarely have trouble constructing or understanding RegExes, but I do have an awful time getting quoting correct and remembering which special characters have to be escaped in any one particular language.
- raiph 11y agoIn Perl 6 the regex character escaping rule is: > Alphanumeric characters and the underscore _ are literal matches. All other characters must either be escaped with a backslash (for example \: to match a colon), or included in quotes. From http://doc.perl6.org/language/regexes http://doc.perl6.org/language/regexes