您不需要正则表达式

by Rina Artstain

通过丽娜·阿斯特斯坦

您不需要正则表达式 (You Don’t Need a Regex for That)

I once had a web siteon which one could searchfor all sorts of things(as long as they’re strings)

我曾经有一个网站,可以搜索各种各样的东西(只要它们是字符串)

The items had titlesand content and moreso the search used a regexto help you explore

这些项目具有标题和内容,而且搜索使用正则表达式来帮助您探索

We had an ideawe gave it a trylet’s help you find morewe’ll add synonyms galore

我们有一个主意,我们给了它一个尝试的帮助,您可以找到更多我们将添加同义词galore

We tacked on those wordsto the end of the regexand straight to production(we didn’t have stress tests)

我们将这些字词加到正则表达式的末尾,然后直接投入生产(我们没有压力测试)

And lo and beholdthe site all but crashedso many timeoutswe all were abashed

瞧,这个站点几乎崩溃了,所以超时了,我们都震惊了

The change was revertedand there we sat hurtedtrying to thinkwhat brought us to this brink

更改被撤消,我们坐在那里痛苦地想着是什么使我们陷入了这种边缘

We researched those regexesand guess what we found:you should compile them firstif you want them around

我们研究了这些正则表达式并猜测我们发现了什么:如果需要它们,请先编译它们

So try that we didand it didn’t workthe site was still slowwe hoped it’s a quirk

所以尝试我们做到了,但没有成功,网站仍然很慢,我们希望这是一个怪癖

We tried many thingswe were all stressinguntil we decidedit’s time to start testing!

我们尝试了很多我们都在强调的事情,直到我们决定是时候开始测试了!

We drew up some schemesfor searching multiple wordsand what a surprisewe saw with our eyes

我们制定了一些搜索多个单词的方案,以及我们眼中看到的惊喜

String.Find()was 1000 times fasterthan all of those regexeswhich caused the disaster

String.Find()比引起灾难的所有正则表达式快1000倍

We learned our lessonI hope you did tooregexes are evilexcept for a few

我们吸取了教训我希望你也做过正则表达式是邪恶的,除了一些

If your regex is simplejust put it awayyou’re probably wastingyour time anyway

如果您的正则表达式简单易用,那么您可能正在浪费您的时间

All of us knowthe truth’s where it’s atin most of the casesYOU DON’T NEED A REGEX FOR THAT

我们所有人都知道,在大多数情况下,真相就在这里。您不需要正则表达式

P.S.Please excuse my bad rhymes and rhythm, I get paid to write software. Not poetry.

PS请原谅我不好的节奏和节奏,写软件我得到报酬。 不是诗歌。

翻译自: https://www.freecodecamp.org/news/you-dont-need-a-regex-for-that-57c771c4fab0/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript正则表达式和Java正则表达式在语法上有一些差异,但是它们的基本概念和用法是相似的。下面是将JavaScript正则表达式转换为Java正则表达式的一些常见规则: 1. 语法差异: - JavaScript正则表达式使用斜杠(/)作为定界符,而Java正则表达式使用双引号(")作为定界符。 - JavaScript正则表达式中的特殊字符需要进行转义,而Java正则表达式中的特殊字符不需要转义。 2. 字符类: - JavaScript正则表达式中的字符类使用方括号([])表示,而Java正则表达式中使用方括号([])或者Unicode转义(\p{...})表示。 - JavaScript正则表达式中的字符类可以使用连字符(-)表示范围,而Java正则表达式需要使用Unicode转义(\uXXXX)表示范围。 3. 量词: - JavaScript正则表达式中的量词使用花括号({})表示,而Java正则表达式中使用花括号({})或者问号(?)表示。 - JavaScript正则表达式中的贪婪量词默认是贪婪模式,而Java正则表达式中的贪婪量词需要在后面添加问号(?)来表示非贪婪模式。 4. 边界匹配: - JavaScript正则表达式中的边界匹配使用插入符号(^)和美元符号($)表示,而Java正则表达式中使用\A和\Z表示。 5. 其他差异: - JavaScript正则表达式中的捕获组使用圆括号(())表示,而Java正则表达式中使用圆括号(())或者方括号([])表示。 - JavaScript正则表达式中的反向引用使用反斜杠加数字(\1、\2等)表示,而Java正则表达式中使用美元符号加数字($1、$2等)表示。 以上是一些常见的JavaScript正则表达式转换为Java正则表达式的规则。具体转换时,还需要根据具体的正则表达式进行适当的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值