Live data from Hacker News

Regexper – Regular expressions visualizer

regexper.com

71–80 of 108 posts

Re: Regexper – Regular expressions visualizer

#71
post #45

Earlier quoted context omitted.

I can't shake the feeling that Regexp could be written just as efficiently as a fluent interface with a more human friendly syntax. I've been telling Jr devs bucking for promotion for years to explain what they're doing in plain english, then write code that looks like that. Basically telling them to skip right over the "gee look what a clever fuck I am" stage and write good code instead of creating riddles. The Rege…

Parsing combinators are the API you want. regexReplaced n = do char 'T' spaces x The above is a Haskell function that does the parsing required above, returning the alphanumeric characters if the parse succeeds and returning an error if it does not. You may not speak Haskell, but this is probably still more readable than (n) => {new RegExp(`T\s (\w{${n}}\w) \s*=`)}, which is the Javascript function that does a simila…

But are they as efficient as regexp? I personally prefer regexp combinators.

Re: Regexper – Regular expressions visualizer

#72
post #40

It's always neat to see where one's ideas go! AFAIK, I was the first person to create dynamic railroad diagrams for regular expressions (maybe 12 or 13 years ago). I got the idea from json.org, which I think was Douglas Crockford's brainchild. My initial implementation was strfriend.com (in Lisp: well under 1,000 lines, including views), and I think its main claim to fame was that Jeff Atwood made fun of it on Twitte…

Railroad diagrams for regular expressions were common long before 2005, and generating such images on demand isn't that unusual, so your work is unlikely to have been a major influence here. Similarly, automatically translating regular expressions from one engine to another is something that people have done before (out of necessity, for compatibility).

Re: Regexper – Regular expressions visualizer

#73
post #30
post #16

The Email::Valid Perl distribution ships with a more than 6000 character regex to validate E-Mail addresses. Both goo.gl and bit.ly refused to shorten it, and when I tried to paste the link here HN refused to accept my comment. But on a machine with Email::Valid installed do: perl -MEmail::Valid -wE 'say $Email::Valid::RFC822PAT' And copy the output into the Regexper form. It takes a while to render, but it'll eventu…

Wow. Despite the utterly insane complexity of a regex of that size, it doesn't seem to do an insane amount of branching. Maximum depth of choices seems to be about 4, which is less than a lot of other regex examples I've seen here. That being said... That regex is just noise. Nobody can tackle it all at once or by themselves, unless they specialise in just regex. It's 6599 characters, at least on my system. So at a w…

Here, have a 1800 line regex which parses perl: https://metacpan.org/source/DCONWAY/PPR-0.000003/lib/PPR.pm#...

Re: Regexper – Regular expressions visualizer

#74
post #41
post #12

Here is a regexp to match an IPv4 address - looks quite nice and easy to understand compared to the regexp! In fact the visualisation makes it easy to spot the mistake. https://regexper.com/#'%5Cb((25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%... (From https://stackoverflow.com/q/5284147/164234 )

The visualization tool shows that the regex is not correct. It allows 000.000.000.000 as an IPv4 address

I don't see the problem. Though not a host address, that's known in the sockets networking API as INADDR_ANY, useful for binding a socket to listening on all networks, for instance.

The GNU C Library getaddrinfo accepts 000.000.000.000 with the leading zeros and all; I just tried.

It is important to support special addresses like 255.255.255.255 and 0.0.0.0 in the dot notation. For instance, in the configuration of some daemon, you may need to be able to specify that the bind address is 0.0.0.0. The value can't be rejected due to not being an address.

Also, you need to be able specify network as opposed to host addresses, and netmasks. You know, like 10.0.0.0 and so on.

Re: Regexper – Regular expressions visualizer

#75
post #53

Earlier quoted context omitted.

Thad first one doesn't seem very good. It seems like there are many places states could be merged. Eg, there are 4 different "^0" states and 3 "^1" states. or am i misreading something?

Amazing how being able to visualize the problem reveals available optimizations!

[deleted]

Re: Regexper – Regular expressions visualizer

#76
post #67
post #30

Earlier quoted context omitted.

Wow. Despite the utterly insane complexity of a regex of that size, it doesn't seem to do an insane amount of branching. Maximum depth of choices seems to be about 4, which is less than a lot of other regex examples I've seen here. That being said... That regex is just noise. Nobody can tackle it all at once or by themselves, unless they specialise in just regex. It's 6599 characters, at least on my system. So at a w…

I can't remember where, but there's some version of that regex somewhere that uses variables for interpolation in subsequent regexes. Viewed like that it's really not that complex, most of it is repetition of previously used regex sequences, it's only when fully expanded that it becomes so humongous.

The pattern is Jeffrey Friedl's, from his book Mastering Regular Expressions.

And as clear as the source could be made, I feel the fact that so many people have just copied and pasted it means that any understanding is lost, and they're just praying and hoping, because a regex of that size is actually difficult for them to comprehend.

Re: Regexper – Regular expressions visualizer

#77
post #53

Earlier quoted context omitted.

Thad first one doesn't seem very good. It seems like there are many places states could be merged. Eg, there are 4 different "^0" states and 3 "^1" states. or am i misreading something?

Amazing how being able to visualize the problem reveals available optimizations!

A regular regex doesn't need to be optimized how it is written if the matcher is dfa based because the minimal dfa is unique. Regex engines however are more complex than that, and the structure this shows isn't going to be how it is actually recognized.

This should reallt only be used for understand what regex matches, not as am optimization tool. In that regard it should display the simplest graph possible to aid understanding.

Re: Regexper – Regular expressions visualizer

#78
post #46

Earlier quoted context omitted.

Yep. I primarily use Go, so you are often forced down this way because it uses a simpler regex engine. I used to complain but in hindsight I realized it was a blessing in disguise. Particularly in Go's case, it has excellent character set library support, especially unicode, so those really tricky corner cases with unicode characters are non-existent now as well. I will be happy if I never see a regex with a unicode…

Which IDE do you use?

I just use a text editor with plugins.

Re: Regexper – Regular expressions visualizer

#79
post #62

Earlier quoted context omitted.

I was taught the algorithms to do this stuff in my Computer Science class over 20 years ago (RE is equivalent to DFA). You weren't the first person to implement regular expression visualizations.

A dynamic visualization on the web 13 years ago? He may have been the first.

It was pretty hard to do viz on the web 13 years ago. In 2006 I made a web tool to turn regular expressions into NFAs and DFAs and animate their states as you typed. It took a lot of code (drawing and animating along beziers, AJAX to a server for graphviz and a regex compilation and minimization package I wrote for this). https://imgur.com/gallery/Yqqoh

These days there’s a lot more tooling and components that can snap together to make this kind of thing.

Re: Regexper – Regular expressions visualizer

#80
post #45
post #37

After 20 years of software development I‘ve come to adopt a best practise: Whenever I start writing a regular expression, I stop and write a „manual“ domain specific parse function instead. Saved me a LOT of debugging time. Since I can now use kotlin pretty much anywhere (jvm, browser, shellscripts) this is easy because of the superb stdlib („startsWith“, „lastIndexOf“, „substringBeforeLast(...)“) The time saved I in…

I can't shake the feeling that Regexp could be written just as efficiently as a fluent interface with a more human friendly syntax. I've been telling Jr devs bucking for promotion for years to explain what they're doing in plain english, then write code that looks like that. Basically telling them to skip right over the "gee look what a clever fuck I am" stage and write good code instead of creating riddles. The Rege…

> I can't shake the feeling that Regexp could be written just as efficiently as a fluent interface with a more human friendly syntax.

You can use SRL - Simple Regex Language (https://simple-regex.com/) for making readable regex/matching rules. It is supported in C++, Java, C#, PHP, Javascript, and Python. Also, you can use the web version to generate equivalent regex if your language is one of the above.

Here is an example from the website for matching an e-mail address:

  begin with any of (digit, letter, one of "._%+-") once or more,
  literally "@",
  any of (digit, letter, one of ".-") once or more,
  literally ".",
  letter at least 2 times,
  must end, case insensitive
Regex to do the same:

  /^(?:[0-9]|[a-z]|[\._%\+-])+(?:@)(?:[0-9]|[a-z]|[\.-])+(?:\.)[a-z]{2,}$/i
The first one is readable, second one is cryptic. https://simple-regex.com/examples has more examples.

SRL was previously discussed here in 2017, see https://news.ycombinator.com/item?id=12384862

Also, the parse feature (DSL) of Rebol language is an excellent regex alternative:

1. Why Rebol, Red, and the Parse dialect are Cool (http://blog.hostilefork.com/why-rebol-red-parse-cool/)

2. Rebol's answer to Regex: parse and Rebol types (https://rebol-land.blogspot.in/2013/03/rebols-answer-to-rege...)

Post reply on HN