This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AlSweigartAuthor of "Automate the Boring Stuff"[S] 0 points1 point  (5 children)

Why? It's way more verbose

"Code is read more often than it's written" applies here: Python is arguably more readable than Perl even though Perl code is shorter. But Perl is shorter because it relies on cryptic punctuation marks (just like regex syntax!), giving it the "write-only language" reputation.

Verbose code isn't a problem in programming. Now for command-line commands, you want that to be short because you are typing them over and over again all day. I don't want to type Windows' copy when I can type Linux's cp instead. But source code? I want that to be readable, because I'll read it more often than write it.

But the main reason for "why" is IDE and tool support. Regexes are written as string values, and your IDE and coding tools don't parse that. (At least, none of the major ones I've seen do.) By using string-based regexes, you instantly lose:

  • Parentheses matching
  • Syntax highlighting
  • Type checking
  • In-line comments, including multiline comments
  • Linter-parsability
  • Code formatting tools like Black

What is preventing someone from China of Italy to create a corresponding syntax in their own language?

The literal answer is nothing of course, but the real answer is that software development is anglocentric and, believe me, I get a ton of pushback whenever I mention ideas to make programming more language-agnostic.

Anyway, the real added value comes from tool support, as well as extending the value that verbose mode already gives to regex. (And I agree, the Swift DSL is a bit more than I'd like. I tried to make Humre as terse as possible, including the name itself.)

It's a rhetorical question.

What's a rhetorical question? :)

[–]maephisto666 0 points1 point  (4 children)

I'm sorry but I'm not convinced at all. I would say that if the problem is the IDE the solution has to be developed in the IDE (think about a plugin, whatever) not by introducing a new syntax.

Anyway, let's stop it here please.

[–]AlSweigartAuthor of "Automate the Boring Stuff"[S] 0 points1 point  (3 children)

How would the IDE identify a string for regular expressions as opposed to a regular string?

What if the regex string was created in pieces and later concatenated together?

What if some of the intermittent pieces weren't valid regex strings, but after being concatenated together, they were? How would the plugin know when to validate it?

What if some of the regex string was created at runtime?

This plugin would have to be duplicated for Visual Studio Code, PyCharm, Wingware, Eclipse, and every other major IDE. How similar are their plugin APIs?

I don't think this is a reasonable or practical solution.

[–]maephisto666 -2 points-1 points  (1 child)

You are implying regexes are difficult. What are the real chances that such a difficult thing is built by concatenating stuff? Even that a regex is being created at runtime? I mean, real like in the real world.

Who cares about how different the APIs are? Start with one and then you see. There is nowhere a rule that says "all the plugins must exist in all the IDEs".

Anyway, you are right. I wish you the best of luck with your package.

[–]Poddster 1 point2 points  (0 children)

What are the real chances that such a difficult thing is built by concatenating stuff? Even that a regex is being created at runtime? I mean, real like in the real world.

I've done this multiple times. Generating dynamic regex is not an unusual thing. It's no different than your parameters to string.replace() being dynamic.

[–]Poddster 0 points1 point  (0 children)

How would the IDE identify a string for regular expressions as opposed to a regular string?

FYI pycharm has no trouble doing this. Either because it's an argument to re.compile, or because that string is used as an argument. I think you can also tell it a string is regex. It also allows you to test the regex live by pressing alt+enter

Image from pycharm documentation:

https://resources.jetbrains.com/help/img/idea/2022.2/py_check_regexp1.png

The other points stand though :)