How important is knowing Regexs?

前端 未结 16 1596
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 05:23

My personal experience is that regexs solve problems that can\'t be efficiently solved any other way, and are so frequently required in a world where strings are as important as

相关标签:
16条回答
  • 2021-02-06 06:21

    Not a brilliant answer but everywhere I've worked the following holds true

    0 < Number of people who (fully) understand regex < 1

    If I knew how to do it I'd write that previous expression as a regex, but I can't. The best I could come up with on the fly is s/fully/a little/g - that's my limit (and that's probably not a regex).

    A more serious answer is that the right regex will solve all kinds of problems, with one(ish) line of code. But you'll have real problems debugging it if it goes wrong. Therefore IMHO a complex regex however 'clean/clever' is a liability, if it takes ten lines of code to replicate it, why's that a problem, is memory/disk space suddenly expensive again?

    BTW I'd love to know if regexs are fast compared to code equivalent.

    0 讨论(0)
  • 2021-02-06 06:23

    It is not clear what kind of answer you are expecting.

    I can imagine roughly three kinds of answer to this question:

    1. Regexen are essential to the education of professional programmers. They enable the use the powerful unix shell tools, and regex-based search-replace can dramatically cut down on text-munging handiwork that is a part of a programmer's life. Programmers that do not know regexen are just intelectually lazy which is a very bad trait for a programmer.

    2. Regexps are kinda useful depending on the application domain. Surely, knowing how to write regexps is a valuable tool a programmer's chest, but most of the time you can do fine without using them. Also, regexps tend to be very hard to read, so abuse must be strongly discouraged.

    3. Some nutcases like to put regexs everything (I'm looking at you, the perl guy who implemented a regex-based tetris in perl). But really, they are just a bit of computer science trivia whose only practical use is in writing parsers. They are widely taught because they make a good teaching topic on which to evaluate students, and like most such topics it can forgotten the second you step out of the exam room.

    You will notice the careful use of the plural forms "regexen" (pro), "regexps" (careful neutral) and "regexs" (con).

    Personally, I am of the first kind. Good programmers like to learn new languages, and they hate repetitive handiwork.

    0 讨论(0)
  • 2021-02-06 06:27

    As a developer you should know the pros and cons of as many tools as possible that could provide pre-made solutions for your problems. Every developer should know how to work with regular expressions and have a feeling when they should be used and when it is besser to use simple string functions to achieve a goal.

    Rejecting them outright because they are hard to read is no option in my opinion. A developer who thinks so strips himself of a valuable tool for searching and validating complex string patterns.

    0 讨论(0)
  • 2021-02-06 06:29

    What does the following do?

    "([A-Za-z][A-Za-z0-9+.-]{1,120}:A-Za-z0-9/{1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)"

    How long did it take you to figure out? to debug?

    Regexs are awesome for single-use throwaway programs, but long hairy regexps are not the best choice for programs that other people will need to maintain over the years.

    0 讨论(0)
提交回复
热议问题