第2集:真实世界的正则表达式

tech2024-05-17  87

Let’s get this out there right off the bat: I love regular expressions. Really, I do — they’re the Swiss Army Knife of text processing, and no respecting developer can go long without needing ’em.

让我们马上解决这个问题:我喜欢正则表达式。 的确,我愿意-它们是文本处理的瑞士军刀,并且没有尊敬的开发人员可以花很长时间而不需要他们。

Of course, we all also know how dangerous they can be. As always, with great power comes great responsibility.

当然,我们所有人都知道它们可能有多危险。 一如既往,强大的力量伴随着巨大的责任。

Still, if you know how — and when — and why — to use regular expressions, they’re indispensable. So this week, regular expressions will be our theme.

不过,如果您知道如何,何时以及为什么使用正则表达式,则它们是必不可少的。 因此,本周,正则表达式将成为我们的主题。

Below are five regular expressions. Each one of them matches a real-world string; that is, a semi-structured piece of text you might want to pull out of a greater document. Here’s an example question to give you an idea what I mean:

以下是五个正则表达式。 它们每个都匹配一个真实世界的字符串。 也就是说,您可能想从更大的文档中提取半结构化的文本。 这是一个示例问题,可让您了解我的意思:

[0-9]{5}

[0-9]{5}

This, of course, is a US ZIP code.

当然,这是美国的邮政编码。

So, what “things” do these regular expressions match? We’ll assume for this quiz that the regex engine is running in case-insensitive mode:

那么,这些正则表达式匹配哪些“事物”? 对于此测验,我们假设正则表达式引擎以不区分大小写的模式运行:

[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}

[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}

&(?!(w+|#d+);)

&(?!(w+|#d+);)

(-?(?:0|[1-9]d*))(.d+)?([eE][-+]?d+)?

(-?(?:0|[1-9]d*))(.d+)?([eE][-+]?d+)?

([da-f]{2}:){5}([da-f]{2})

([da-f]{2}:){5}([da-f]{2})

<[^>]*?>

<[^>]*?>

Of course, since we’re dealing with regular expressions here, I’d be amiss if I didn’t give you two problems for the price of one.

当然,由于我们在这里处理正则表达式,所以如果我不以一个价格给您两个问题,我将感到不满意。

In each case, the regular expression has something wrong with it. For example, the ZIP code regex above doesn’t correctly match the ZIP+4 format (i.e. 66044-0034) that’s used for many addresses these days.

在每种情况下,正则表达式都有问题。 例如,上面的邮政编码正则表达式与当前用于许多地址的ZIP + 4格式(即66044-0034)不正确匹配。

So, for part two, what’s wrong with the rest of ’em?

那么,对于第二部分,剩下的它们又有什么问题呢?

Enjoy your Thanksgiving belly-stuffing, and tune in over the weekend for the answers.

享受感恩节的肚子,并在周末收听答案。

翻译自: https://www.sitepoint.com/episode-2-real-world-regular-expressions/

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)