正则表达式是编程必须的吗?

发布于 2024-07-21 06:26:24 字数 20 浏览 7 评论 0原文

正则表达式是编程必须的吗?

Are Regular Expressions a must for doing programming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

不再见 2024-07-28 06:26:24

人们可以很容易地没有它们,但人们应该(恕我直言)了解基础知识,原因有两个。
1) 也许有一天,正则表达式是解决当前问题的最佳解决方案(见下图)
2) 当你在别人的代码中看到正则表达式时,它不应该是 100% 神秘的。

preg_match('/summarycount">.*?([,\d]+)<\/div>.*?Reputation/s', $page, $rep);

这段代码很简单,但如果你不知道正则表达式,那么第一个参数中的东西也可能是火星人语言。 一旦您学习了基础知识,这里使用的正则表达式实际上非常简单,为了让您走得更远,请前往 http ://www.regular-expressions.info/ 他们有很多关于正则表达式及其在不同平台/语言上的各种实现的信息,他们还有一个很棒的入门教程。 之后查看 RegexBuddy,它可以帮助您构建 RegEx,在构建它们时如果您观察它的作用然后它可以帮助你瘦身,这是迄今为止我花过的最好的 39.95 美元。



原创漫画

One could easily go without them but one should (IMHO) know the basics, for 2 reasons.
1) There may come a time where RegEx is the best solution to the problem at hand (see image below)
2) When you see a Regex in someone else's code it shouldn't be 100% mystical.

preg_match('/summarycount">.*?([,\d]+)<\/div>.*?Reputation/s', $page, $rep);

This code is simple enough but if you don't know RegEx then that stuff thats in the first parameter may as well be a Martian language. The RegEx thats used here is actually pretty simple once you learn the basics, and to get you that far head over to http://www.regular-expressions.info/ they have ALOT of info about RegEx and its various implimentations on the different platforms/langauges they also have a great tutorial to get started with. After that check out RegexBuddy, it can help you build RegExs and while you build them if you watch what it does then it can help you lean, it by far was the best $39.95 I've ever spent.




Original Comic

尛丟丟 2024-07-28 06:26:24

是的。 您可以在没有它们的情况下进行管理,但您确实应该至少学习基础知识,因为大多数计算任务都可以使用它们。 从长远来看,你会省去很多痛苦和麻烦。 一旦你度过了最初的“wtf”阶段,正则表达式比你想象的要容易得多。

Yes. You can manage without them, but you really should learn at least the basics as most computing tasks could use them. You will save a lot of pain and hassle in the long run. Regex's are much easier than you think once you get over the initial 'wtf' stage.

倾其所爱 2024-07-28 06:26:24

我想说不,它们不是必须的。 即使不了解它们,你也可以成为一名优秀的程序员。

我发现我主要将正则表达式用于数据操作的一次性任务,而不是实际放入应用程序代码。 它们可以很方便地验证输入数据,但如今您的控件通常会为您做这件事。

I would say no, they are not a must. You can be a perfectly good programmer without knowing them.

I find I use Regular Expressions mostly for one-off tasks of data manipulation rather than for actually putting in application code. They can be handy for validating input data but these days your controls often do that for you anyway.

-黛色若梦 2024-07-28 06:26:24

一点也不。 使用正则表达式可以做的任何事情,完全可以不用它们来完成。

然而,它是一个强大的模式匹配系统,因此一些使用简单的正则表达式模式很容易完成的事情,在没有它的情况下需要大量代码才能完成。

例如,

s = Regex.Replace(s, "[bcdfghjklmnpqrstvwxz]", "$1o$1");

如果没有正则表达式,需要更多的代码:

StringBuilder b = new StringBuilder();
foreach (char c in s) {
   if ("bcdfghjklmnpqrstvwxz".IndexOf(c) != -1) {
      b.Append(c).Append('o').Append(c);
   } else {
      b.Append(c);
   }
}
s = b.ToString();

或者,如果您不是经验丰富的程序员,您可以轻松创建更多代码并且性能非常糟糕的东西:

string temp = "";
for (int i = 0; i < s.Length; i++ ) {
   if (
      s[i] == 'b' || s[i] == 'c' || s[i] == 'd' ||
      s[i] == 'f' || s[i] == 'g' || s[i] == 'h' ||
      s[i] == 'j' || s[i] == 'k' || s[i] == 'l' ||
      s[i] == 'm' || s[i] == 'n' || s[i] == 'p' ||
      s[i] == 'q' || s[i] == 'r' || s[i] == 's' ||
      s[i] == 't' || s[i] == 'v' || s[i] == 'w' ||
      s[i] == 'x' || s[i] == 'z'
   ) {
      temp += s.Substring(i, 1);
      temp += "o";
      temp += s.Substring(i, 1);
   } else {
      temp += s.Substring(i, 1);
   }
}
s = temp;

Not at all. Anything that you can do with regular expressions is entirely possible to do without them.

However, it's a powerful pattern matching system, so some things that is quite easy to accomplish with a simple regular expression pattern takes a lot of code to do without it.

For example, this:

s = Regex.Replace(s, "[bcdfghjklmnpqrstvwxz]", "$1o$1");

needs a bit more code to do without a regular expression:

StringBuilder b = new StringBuilder();
foreach (char c in s) {
   if ("bcdfghjklmnpqrstvwxz".IndexOf(c) != -1) {
      b.Append(c).Append('o').Append(c);
   } else {
      b.Append(c);
   }
}
s = b.ToString();

Or if you are not quite as experienced a programmer, you could easily create something that is even more code and performs horribly bad:

string temp = "";
for (int i = 0; i < s.Length; i++ ) {
   if (
      s[i] == 'b' || s[i] == 'c' || s[i] == 'd' ||
      s[i] == 'f' || s[i] == 'g' || s[i] == 'h' ||
      s[i] == 'j' || s[i] == 'k' || s[i] == 'l' ||
      s[i] == 'm' || s[i] == 'n' || s[i] == 'p' ||
      s[i] == 'q' || s[i] == 'r' || s[i] == 's' ||
      s[i] == 't' || s[i] == 'v' || s[i] == 'w' ||
      s[i] == 'x' || s[i] == 'z'
   ) {
      temp += s.Substring(i, 1);
      temp += "o";
      temp += s.Substring(i, 1);
   } else {
      temp += s.Substring(i, 1);
   }
}
s = temp;
睫毛溺水了 2024-07-28 06:26:24

让我这样说,如果你的工具包中有正则表达式,你会节省大量的时间和精力。 如果你没有它们,你将不知道你错过了什么,所以你仍然会很快乐。

作为一名网络开发人员,我经常使用它们(输入验证、从网站提取数据等)。

编辑:我意识到通过查看 正则表达式标签 可能会帮助您了解正则表达式用于解决的一些常见问题 就在 stackoverflow 上。

Let me put it this way, if you have regular expressions in your toolkit, you'll save yourself a lot of time and energy. If you don't have them, you won't know what you're missing out on so you'll still be happy.

As a web developer, I use them very often (input validation, extracting data from a site etc).

EDIT: I realized it might help you to look at some common problems that regex is used for by looking at the regex tag right here on stackoverflow.

随波逐流 2024-07-28 06:26:24

我会说是的。

它们是如此普遍有用,以至于完全没有至少阅读和阅读的能力是一个相当大的障碍。 写简单的。

支持正则表达式的语言

  • Java
  • perl
  • python
  • PHP 。
  • C#
  • Visual Basic.NET
  • ASP
  • powershell
  • javascript
  • ruby
  • ​​ tcl
  • vbscript
  • VB6
  • XQuery
  • XPath
  • XSD
  • MySQL
  • Oracle
  • PostgreSQL

支持正则表达式的 IDE 和编辑器

  • Eclipse
  • IntelliJ
  • Netbeans
  • Gel
  • Visual Studio
  • UltraEdit
  • JEdit
  • Nedit
  • Notepad++
  • Editpad Pro
  • vi
  • emacs
  • HAPEdit
  • PSPad

我们不要忘记 grep< /code> 和 sed

作为雇主,您希望拥有一个优秀的程序员,偶尔需要在数千个源文件中手动查找/替换一些类似的字符串,并且需要数小时或数天才能完成,或者是一个优秀的程序员偶尔花五分钟甚至十分钟编写一个正则表达式来完成与他们去喝咖啡的时间相同的事情?

这个答案中的真实世界实际用法

事实上,我在撰写这篇文章时实际上使用了正则表达式。 我最初以逗号分隔的散文形式列出了支持它的语言。 然后我重新考虑,并通过在 JEdit 中搜索表达式 (\w+), 并将其替换为 \n* $1,将格式更改为项目符号列表。 您获得的经验越多,对于越来越短的动作组来说,使用它们就会变得越来越划算。

I would say yes.

They're so universally useful that it's a pretty significant handicap to be entirely without the ability to at least read & write simple ones.

Languages that Support Regular Expressions

  • Java
  • perl
  • python
  • PHP .
  • C#
  • Visual Basic.NET
  • ASP
  • powershell
  • javascript
  • ruby
  • tcl
  • vbscript
  • VB6
  • XQuery
  • XPath
  • XSDs
  • MySQL
  • Oracle
  • PostgreSQL

IDEs and Editors that Support Regular Expressions

  • Eclipse
  • IntelliJ
  • Netbeans
  • Gel
  • Visual Studio
  • UltraEdit
  • JEdit
  • Nedit
  • Notepad++
  • Editpad Pro
  • vi
  • emacs
  • HAPEdit
  • PSPad

And let's not forget grep and sed!

As an employer, which would you rather have, a good programmer that - once in a while - will have to manually find/replace some set of similar strings across thousands of source files and require hours or days to do it, or a good programmer that - once in a while - spends five, or even ten minutes crafting a regex to accomplish the same thing that runs in the time it takes them to go get some coffee?

Real World Practical Usage in this very Answer

In fact, I actually used a regex in crafting this post. I initially listed the languages that support it in comma delimited prose. I then rethought it and changed the format to a bulleted list by searching for the expression (\w+), and replacing it with \n* $1 in JEdit. And the more experience you get with them, using them will become more and more cost effective for shorter and shorter sets of actions.

邮友 2024-07-28 06:26:24

不会。您可以在不接触正则表达式的情况下编程多年。 当然,这意味着在某些情况下,知道 RE:s 的人会使用它们,你会做其他事情。 解决特定问题总是有不止一种方法,而正则表达式只是表达模式的一种方法(一种非常有效,因此可能很流行的方法)。

No. You can be programming for years without touching regular expressions. Of course it will mean that for some cases where someone who knows RE:s would use them, you would do something else. There is always more than one way to solve a particular problem, and regular expressions is just one way (a very efficient, and perhaps therefore popular way) of expressing patterns.

榕城若虚 2024-07-28 06:26:24

如果您关心发展软件工程师的职业生涯,那么是的。 我雇用软件工程师,如果他们不了解使用正则表达式的基础知识,或者从未听说过它们,那么我想知道他们在整个编程技术领域实际上拥有多少经验。 还有什么是他们不知道的?

上面的大多数评论都说“不,你可以用其他方式解决问题”,他们也大多说替代方案是更多代码并且需要更长的时间来编写......现在想想可维护性以及更改此定制代码是多么容易。 .. 使用正则表达式 - 那么它只是一行代码。

If you care about developing a career as a software engineer, then yes. I hire software engineers and if they don't know the basics of using regular expressions, or have never heard of them, then I wonder how much experience they actually have across the entire spectrum of programming techniques. What else don't they know?

Most of the comments above say 'no, you can solve the problem in other ways' and they also mostly say the alternatives are more code and take longer to write... now think maintainability and how easy this bespoke code would be to change... Use a regular expression - then it's just a single line of code.

荒人说梦 2024-07-28 06:26:24

至少知道正则表达式的存在以及它们的用途是绝对必须的。 否则,在许多情况下你将面临重新发明轮子的危险。 如果您知道它们的存在,那么您可以在必须应用它们时详细了解它们。
顺便说一句,正则表达式背后的理论非常有趣:-)

At least knowing that regular expressions exist and what they can be used for is an absolute must. Otherwise you will be in danger of reinventing the wheel in many situations. If you know about their existence you can go into the details once you have to apply them.
BTW, the theory behind regular expressions is quite interesting :-)

罪#恶を代价 2024-07-28 06:26:24

Jeffrey Friedl 写了一本很棒的书,名为掌握正则表达式。 它给了我洞察力,读起来真是一种乐趣。

尽管我不经常使用正则表达式,但它们最近派上了用场:

  • 输入:一些 CSV 字典文件,具有某种松散格式、多种翻译、谚语等。

  • 输出:漂亮的 JSON。

  • 第一个想法:编写一个简短的语法来解析所有可能的字段和值。

  • 第一次尝试:写了一个语法,但有一些粗糙的边缘,主要是特殊情况,只出现在 0-1% 的数据中。 制定一个涵盖所有内容的语法就显得设计太多了。

  • 第二次尝试:我使用了一个简单的语法来捕获主要字段,然后将其余部分传递给一个例程,该例程应用了一些正则表达式。 它速度快,概念上比完整的语法更容易,而且写起来也很有趣。

  • 总结:正则表达式节省了我的时间,并且实际上帮助我了解数据中的特殊情况以及它们出现的方式和位置。

它们值得学习吗? 是的。

必须吗?不,但据我所知,该领域几乎没有人不熟悉它们。

难学吗?一点也不难。

There is a great book out there written by Jeffrey Friedl called Mastering Regular Expressions. It gave me insight and was a real joy to read.

Even though I do not use regexes that often, they recently came in handy:

  • Input: Some CSV dictionary file with some kind of loose format, multiple translations, sayings, etc.

  • Output: Nice JSON.

  • First thought: Write a short grammar to parse all possible fields and values.

  • First attempt: Wrote a grammar, but there were some rough edges, mainly special cases, which occured in just 0-1% of the data. Making a grammar that catches all would have been too-much-design.

  • Second attempt: I used a simple grammar catching the main fields and then passed over the rest to a routine, which applied some regular expressions. It was fast, conceptually easier than a full grammar and fun to write, too.

  • Summary: Regular expressions saved me hours and actually helped me seeing the special cases in the data and how and where they appeared.

Are they worth learning? Yes.

A must? No, but I know almost no one in the field whose not familiar with them.

Difficult to learn? Not at all.

雨轻弹 2024-07-28 06:26:24

总之,不。

但它们肯定是适合正确工作的正确工具,并且对于那些最有效的字符串匹配操作值得学习。 然而,仅仅因为你有一把又好又大的锤子,并不意味着你应该用它来敲碎所有坚果。

In a word, No.

But they can certainly be the right tool for the right job and are worth learning for those string matching operations where they work best. However, just because you've got a good, big hammer, it doesn't mean you should use it to crack every nut.

栖迟 2024-07-28 06:26:24

不,我自己在正则表达式方面很糟糕,但我仍然是一个糟糕的程序员。 等待。 什么?

更严肃地说:我知道正则表达式,但几乎不需要它们。 如果我确实需要一个,例如当我需要像戴夫提到的那样验证用户输入时,我会询问一位同事。

作为一名程序员,有很多东西值得了解/学习,但我敢说正则表达式远远不是该列表的顶部。

No. I'm terrible at regular expressions myself, and still I'm a bad programmer. Wait. What?

On a more serious note: I don't know regular expressions, but hardly ever need them. If I really need one, for instance when I need to validate user input like Dave mentions, I ask a colleague.

There are so many things that are valuable to know / learn as a programmer, but I'd dare say regular expressions is far from being anywhere near the top of that list.

简单 2024-07-28 06:26:24

实际上,我的感觉是这是必须的.​​..

例如,我正在研究为什么我们的 YouTube 视频的一部分不起作用...结果发现这些视频的链接是

http://ca.youtube.com/v/raINk2Ii1A4(不是实际网址,仅作为示例)

而不是

< a href="http://www.youtube.com/v/raINk2Ii1A4" rel="nofollow noreferrer">http://www.youtube.com/v/raINk2Ii1A4

另一位程序员早些时候使用了“substr() ”提取youtube视频ID,由于ca.youtube.com部分,ID提取错误。

所以在我看来,正则表达式非常重要,如果没有正则表达式,隐藏的错误可能会比平时更频繁地出现。

但我之前其实见过3位开发者,其中一位是非常优秀的Web应用程序
一名开发人员,其中一位拥有著名硅谷顶尖大学,其中一位是高调的硕士毕业生,结果他们都不懂常规表达式。 这让我有点惊讶。

Actually, my feeling is that it is a must...

For example, I was looking at why a portion of our YouTube video didn't work... and it turned out the links for those videos are

http://ca.youtube.com/v/raINk2Ii1A4 (not actual URL, just as an example)

instead of

http://www.youtube.com/v/raINk2Ii1A4

Another programmer earlier used "substr()" to extract the youtube video ID, and because of the ca.youtube.com portion, the ID was extracted wrong.

So to my feeling, regular expressions are very important and without that, hidden bugs can be introduced more often than usual.

But I actually met 3 developers before, one was a very good web applications
developer, one had a Master of Science degree from a prestigious Silicon Valley top university, and one was a high-profile master grad, and it turned out they all didn't know regular expressions. That was a bit surprising to me.

瀞厅☆埖开 2024-07-28 06:26:24

嗯,在计算机科学理论领域,它是非常强大且有用的“设备”,因为有了它,您可以定义常规语言并识别它的 NFA 甚至 DFA,从而证明计算理论或有限自动化和形式语言领域中的一些困难定理。
在实际编程中它也非常有用,因为使用它您可以以相对简单的方式执行复杂的字符串操作。

Well, in computer science theoretical field it's very strong and useful "equipment", since with it you are able to define regular languages and identify with it NFA or even DFA, therefore prove some difficult theorem in computation theory or finite automate and formal languages field.
In practical programming it's very useful as well, since using it you are able to perform a complex string manipulation in relative easy way.

花落人断肠 2024-07-28 06:26:24

可能不会。 但它们非常容易学习。 至少基础知识(所有正则表达式引擎所做的事情)很快就能学会。 我在聊天窗口中从另一个人那里花了大约 30 分钟学会了它......

Probably not. But they are really easy to learn. At least the basics (the stuff all the regex engines do) are quickly taught. I learnt it in a chat window from another guy in like 30 minutes...

半葬歌 2024-07-28 06:26:24

我想这不是必须的,但它们会减轻你的生活并节省你很多时间。

如果您不知道如何使用正则表达式,您就不知道自己错过了什么。 但只要看到一个人使用它们来完成一项任务,你就会觉得这是你绝对应该拥有的一项技能。

I guess it is not a must but they will ease your life and save you so much time.

If you dont know how to use regular expressions you dont know what you are missing. But just looking at a person using them to complete a task makes you feel that it is a skill you should definitely have.

坦然微笑 2024-07-28 06:26:24

不...是的,

这很像“我应该学习 C”问题之一。 没有正则表达式不一定是做某事的唯一方法。 但它们通常是一种有用的抽象,可以简化代码,并且(我真的认为)甚至可以使其更具可读性。 也许是因为我喜欢 Jeff Friedl 的《掌握正则表达式》 或者可能是因为我喜欢在 Perl 中分配。 但无论出于何种原因,正则表达式都是我的首选工具。 现在对我来说使用正则表达式似乎比大多数其他字符串操作技术更容易。

No... and Yes,

This is very much like one of those, "Should I learn C" questions. No regular expressions are never necessarily the only way to do something. But they are often a helpful abstraction that simplifies code and can (I really think) even make it more readable. Maybe is because I love Jeff Friedl's Mastering Regular Expressions or maybe its because I do allot at work in perl. But for whatever reason regular expressions are my go to tool. It now seems easier for me to use a regex then most other string manipulation techniques.

我也只是我 2024-07-28 06:26:24

至少在最低层面上理解正则表达式是什么/可以做什么是非常重要的。 如果您了解 NFA 背后的概念,那么您将更好地理解其他问题。

至于开始擅长正则表达式,我认为没有必要,但确实很有价值。 事实上,每个正则表达式引擎都是不同的,因此即使您已经掌握了一个正则表达式引擎,您也可能无法在其他地方快速做到这一点。

Understanding at least at the lowest level what regular expressions are/can do is immensely important. If you understand the concepts behind and NFA then you will understand other problems much better.

As for begin good at Regular Expressions, I would say not necessary but really valuable. The fact is every Regular expression engine is different, so even if you've mastered one you may not be able to quickly do it elsewhere.

人间☆小暴躁 2024-07-28 06:26:24

正则表达式即使不使用,至少学习也很重要。

首先,你必须能够阅读并理解别人的正则表达式代码。

其次,基本正则表达式对应于有限自动机(根据克莱恩定理),这使得它们对于算法设计至关重要。

实际上,有一款适合女孩的备忘单裙子

http://store. xkcd.com/xkcd/#RegexCheatSkirt

如果您碰巧是女孩,这可能是一个绝佳的学习机会。

Regular expressions are important at least to learn if not to use.

First, you must be able to read and understand others' regular expression code.

Second, basic regular expressions correspond to finite automata (by the Kleene theorem), which makes them fundamentally important for algorithm design.

Actually, there is a cheat sheet skirt for girls

http://store.xkcd.com/xkcd/#RegexCheatSkirt

If you happen to be a girl, this might be a fantastic learning opportunity.

平生欢 2024-07-28 06:26:24

不,您始终有两种其他选择可以满足合适的要求。

  1. 询问了解正则表达式的朋友。

  2. 在 SO 上发布问题。

No, you always have two other options for suitable requirements.

  1. Ask a friend who knows regexes.

  2. Post the problem on SO.

痴者 2024-07-28 06:26:24

根据您的领域,某些问题适合正则表达式 - 或者相反:/不/使用正则表达式的解决方案非常笨拙。 我想到了电子邮件验证/URL 验证/最小密码强度/日期解析。

Depending on your field there are certain problems that lend themselves to regexes - or rather the other way around: the solution /not/ using regular expressions is extremely clumsy. email verification/url verification/minimum password strength/date parsing come to mind.

乖不如嘢 2024-07-28 06:26:24

一定不是。 尽管人们认为优秀的程序员应该知道这一点,但我不会这么说。 当时机到来并且您需要它时,您只需使用它即可。 无论如何,如果六个月不使用它,您将不会记住任何表达选项。

就像编程中的所有事实一样,你学习了它,你忘记了它,你又重新学习了它。

Must it is not. Though there is come perception that a good programmer should know it, i wouldn't say so. When the time comes and you'll need it, you'll just use it. Anyway, give it a six months not using it and you won't remember any expression options.

Like everything factual in programming, you learn it, you forget it, you relearn it again.

永言不败 2024-07-28 06:26:24

不会。

根据您想要实现的目标,正则表达式可能很有用。 但我敢说,80% 或更多的程序员从不使用 Regex,大约 15% 左右的程序员只是偶尔使用(并且必须 Google 一下),而剩下的只有一小部分人真正使用过 Regex Ninjas。

我发现 Regexr 对于我很少使用 Regex 的情况来说非常好。

另外,有人会在接下来的一分钟左右提到jwz的某句话......

No.

Depending on what you're trying to achieve, Regex can be useful. But I would hazard that 80% or more of programmers never use Regex, some 15% or so only occasionally (and have to Google it) and only a small % of the remainder ate actually Regex Ninjas.

I have found Regexr is pretty good for the rare occasions I use Regex.

Also, someone will mention a certain quote from jwz within the next minute or so...

作妖 2024-07-28 06:26:24

正则表达式是一种功能强大的模式匹配语言。 而且它不限于文本字符串。 但一如既往,你的代码,你的电话。

Regular Expressions is a powerful pattern matching language. And it is not limited to text strings. But as always, your code, your call.

椵侞 2024-07-28 06:26:24

简单地说,不。 这完全取决于您的计划要实现的目标。

当然,了解 RegExp 是什么以及对其工作原理的基本了解在将来会很有用。

Simply, no. It all depends on what your program is set out to achieve.

Of course knowing what a RegExp is and a basic understanding of how they work can be useful in the future.

方圜几里 2024-07-28 06:26:24

我同意其他人的观点,这可能不是必须的,但至少有一个基本的理解是非常有帮助的。 我在我的多维数据集中发布了一份 RegEx 备忘单,我发现它非常有帮助。http://regexlib.com/CheatSheet。 ASPX

I agree with the others that it's probably not a must, but it's very helpful to have at least a basic understanding. I have a RegEx cheat sheet posted in my cube that I find very helpful.http://regexlib.com/CheatSheet.aspx

一城柳絮吹成雪 2024-07-28 06:26:24

理解正则表达式并不是必须的。 然而,它是处理文本的有效工具。 如果您从事操作文本的项目,您最终会遇到它们。

正则表达式会带来各种挑战,无论您是使用它们还是只是支持包含它们的代码。 请注意,有多种语法风格。 不同的库和语言通常具有略有不同的语法规则。 正则表达式随着变得越来越复杂,可以很容易地从一个简单的模式匹配工具转变为一个魔法,只编写不容易理解的代码。 而且,与大多数文本处理工具一样,它们通常很难进行故障排除或更改(例如,您有一个不再适合该工具功能的极端情况)。

与所有解析代码一样,我建议进行大量单元测试。 特别要注意边缘条件、重复的文本模式和异常输入。

Understanding regular expressions is not a must. However, it is an effective tool for processing text. If you work on projects that manipulate text, you will eventually run across them.

Regular expressions come with a variety of challenges, whether you are using them or just supporting code that has them. Be aware that there are a variety of syntax flavors. Different libraries and languages often have slightly different syntax rules. Regular expressions, as they become more complicated can easily transition from a simple pattern matching tool to a piece of magic, write only code that cannot be easily understood. And, like most text processing tools, they can often be difficult to troubleshoot or change (e.g. you have a corner case that no long fits the features of the tool).

As with all parsing code, I recommend a lot of unit tests. In particular, watch out for edge conditions, repeated text patterns and unusual inputs.

动次打次papapa 2024-07-28 06:26:24

绝对不是,我(和很多人一样)已经编程多年但没有接触过它们。
也就是说,一旦您了解它们,您就会开始看到它们过去可能有用的地方:-)

我想说 - 只需阅读基础知识,这样您就知道 RegEx 是什么以及您可以用它们做什么,然后,如果您发现它们可能有用,您可以获取教程/参考网站,例如 http://www.regular -expressions.info/ 并直接跳入。

Definitely not, I (like many people) have been programming for years without touching them.
That said, once you get to know them you start to see where they might have been useful in the past :-)

I'd say - just read up on the basics so you know what RegExes are and what you can do with them, then if you ever find they might be useful you can grab a tutorial / reference website like http://www.regular-expressions.info/ and jump right in.

陪你搞怪i 2024-07-28 06:26:24

如果您正在开发新产品,我建议您避免使用它们,或者最多谨慎而明智地使用它们。

如果您正在维护一个已经使用正则表达式的产品,您将别无选择。

它至少有助于识别正则表达式,因此,如果您遇到一段特别混乱的代码,您就知道找到参考卡的正确搜索词。

If you're developing a new product, I would suggest you avoid them, or at the very most use them sparingly and judiciously.

If you're maintaining a product that already uses regexp's you are left with no choice.

It helps to atleast be able to recognize a regular expression so if you encounter a particularly obfuscated piece of code you know the right search term to find a referance card.

破晓 2024-07-28 06:26:24

只不过是了解 HTML 或能够使用关系数据库。 严格来说,不,它们不是进行编程的必要条件——它们可能在某些工作中是必不可少的和基础的,但在其他工作中却无关紧要。 在为新的以太网芯片编写设备驱动程序时,您不太可能使用正则表达式(或 HTML 或 SQL)。 在我的领域,我偶尔会在生产代码中使用正则表达式,更常见的是在临时脚本中使用正则表达式来处理报告等。我曾参与过一个项目,其中正则表达式是一个核心功能(一个用于分析自由格式文本以查找内容的应用程序)某些关键短语来生成编译的规则集)。

No more so than, say, knowing HTML or being able to use a relational database. Strictly speaking, no, they're not a requirement for doing programming--- they might be essential and fundamental in some jobs, and yet irrelevant in others. You're unlikely to use regular expressions (or HTML or SQL, for that matter) while writing a device driver for a new Ethernet chip. In my area I use regular expressions occasionally in production code, much more often in ad-hoc scripts to massage reports etc. I've worked on one project where they were a central feature (an application to analyse free-form text to look for certain key phrases to produce a compiled rule set).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文