TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

RegExpBuilder: because regular expressions suck

2 点作者 BobTurbo将近 12 年前

3 条评论

gregjor将近 12 年前
[The response from BobTurbo that I replied to below disappeared while I was writing this. In his comment he assumed that I must want every programming language to use symbols instead of methods and functions.]<p>You can assume whatever you want but that won&#x27;t make for a productive conversation. I didn&#x27;t say anything about &quot;entire programming language syntax,&quot; I was talking about regular expressions. In other contexts -- jQuery is a good example -- techniques like yours actually help because they improve over the alternative. In the case of regular expressions I&#x27;m not persuaded. I&#x27;ll give a few reasons.<p>1. Hundreds of thousands of programmers have learned and used regexes since the 1960s. regexes have been implemented in too many languages, libraries and tools to count. That didn&#x27;t happen because they &quot;suck&quot; but no one has been smart enough to improve on them until now. That happened because they are an efficient, succinct, well-understood and tested way to do the job.<p>2. Programmers can find lots of good documentation on regexes, there are many good tools for building and testing them, and they are built-in to the syntax of quite a few languages (including Javascript and Perl). If I come across a regex in code I&#x27;m working on I know where to go to figure it out and test it. If I come across your code I will have to learn something new and wonder if it does what I think it&#x27;s doing.<p>3. I can&#x27;t copy&#x2F;paste your code into a regex validator or tester. I can&#x27;t use the same regex in Javascript and PHP unless&#x2F;until you have a PHP version (for example).<p>4. Regular expression syntax is part of the IEEE POSIX standard, although there are quite a few variations and implementations.<p>5. I&#x27;m much more likely to trust the correctness of regex libraries that have been around and in use for 20 years or more than an add-on library I found on github.<p>6. To build a non-trivial regex with your library I still need a fairly good understanding of the things that catch people up: backtracking, grouping, subexpressions, etc. The same problem comes up with the various attempts to simplify SQL or hide it behind an ORM -- by hiding complexity you add a layer of difficulty to understanding and debugging that wouldn&#x27;t be there if you just mastered the hard sucky thing in the first place.<p>7. Regexes are taught (or should be taught) to illustrate finite state machines and regular grammars. Learning how regexes work is rewarding independent of specific implementations in programming languages.<p>8. As I already mentioned I am 99.999% more likely to come across a regular expression in it&#x27;s usual form (the one that sucks) than in the form you&#x27;ve adopted. That may be in Java, Python, or Javascript code, a text editor, an AWK script, or an Apache config file. I have no incentive for learning another syntax for regular expressions since it is not going to replace the widely-used syntax.<p>There&#x27;s a little irony here: you must have learned regular expressions at a fairly deep level to write your library, so you probably don&#x27;t need it for your own projects.<p>No personal offense intended. You post on Hacker News, people are going to add their opinions. You can agree or not. Good luck with your library.
gregjor将近 12 年前
Dismissing a mature and widely-used tool by saying &quot;it sucks&quot; doesn&#x27;t inspire me to read more, but I did. Personally I think it&#x27;s easier to learn, read and maintain &#x2F;\$[0-9]+\.[0-9][0-9]&#x2F; rather than a bunch of new Dart&#x2F;Javascript&#x2F;Python&#x2F;Java methods. It&#x27;s certainly more efficient. But I&#x27;ve been using regexes since before any of those languages were invented.<p>You&#x27;re going to encounter regexes in other contexts -- text editors, Unix&#x2F;Linux utilities, databases, code you didn&#x27;t write -- and if you can only use them with training wheels you&#x27;re stuck.
评论 #6255327 未加载
tudborg超过 11 年前
It doesn&#x27;t seem to simplify regex, just make it more verbose. That might be fine, except the people who need to understand how to use it, must already understand the concepts of regexes.<p>And if you can use this library without understanding regex, you might be better of not using regex at all.<p>Regex looks complex at first glance, but with a good cheatsheet and tool you should have no problem.<p><a href="http://gskinner.com/RegExr/" rel="nofollow">http:&#x2F;&#x2F;gskinner.com&#x2F;RegExr&#x2F;</a> is a great tool for building and testing regexes.