Lurker News

Comments (108) 46 threads

m4637 replies#
I think what regex's need is a really powerful syntax and language aware regex editor.

I've been using regexs for most of my career, and still struggle to get them right on first writing.
The #1 problem I run into is:
what is a literal character and what is a control character?
for example, both these are very common:
- match a parenthesis character or a period character
- use a parenthesis to group a match or use a period to match any one character
You would think I would learn it once, and be good.
but my #2 problem confounds this:
what is a literal character and what is a control character - in the language I am using?
for example I might need to escape a period to make it a literal for a regex.
If I am checking the files filexc and file.c and want to match the second, the regex I want is

  ^.*\.c$
in perl, I could say:

  $rx = "^.*\\.c\$";    ($" is a thing)
  if ($f =~ /$regex/) { ...
better would be:

  if ($f =~ /^.*\.c$/) {...
in python I would write

  m = re.search("^.*\\.c$",f)
better would be:

  m = re.search(r'^.*\.c$', f)
in a shell script, I might say:

  grep "^.*\\.c$"
EDIT: crap, I had to escape my comment because the asterisk in the regex was making my text italic
Gehinnn#
Do you think an extension for vscode might be worth it?
squeaky-clean#
I use regexbuddy and it does a lot of this. Huge downside is that it's $40 and Windows only.

You can do things like write a more generic regex and then select your language (e.g Python 2 or 3, Java, Perl, so on), And a few common actions, such as "iterate over all matches in a string" and it will auto-generate a code stub for you. Whenever teammates of mine are working on a weird regex they usually email me to double check it for them. (My response is usually that they're trying to do too much with one regex, haha)

m463#
pretty good, for problem #1

actually, very good.
doesn't seem to address problem #2 unless I missed something

77866551 reply#
I don't see the problem here. The regex itself is exactly the same, it's just that different languages have different string literal syntaxes (and some have dedicated regex syntax, thus solving the problem of double-escaping).

The only regex engine where this is a problem is Vim's, because there are characters that a special unless escaped, and characters that are normal but become special when escaped. And as if that wasn't enough, there are config options to determine which characters those are. My usual practice is to prefix all Vim regexes with \v so that all the special characters are at least consistent.

m463#
> I don't see the problem here.

I think the problem is that it's like shooting off a boat in choppy waters. You not only have to decide what you're aiming at (problem #1), you have to anticipate how the motion of the boat will affect it (problem #2).

mycall#
RegExBuddy comes to mind.
dntbnmpls3 replies#
The biggest problem I've noticed for regex is we use it every once in a while and once it works we move onto other things. And a few weeks/months later, we have forgotten much of it and have to relearn it all over again. Whereas, you generally use your programming language ( C++, C#, Java, etc ) everyday to keep your skills sharp, regex is generally "set it and forget it" situation for most people. And as you noted, different languages/shells/etc implement their own flavor of regex which can trip you up.

It's similar to SQL when you think about it. You set up a query to get the data you need and move on to other things. And every RDBMs implements their own flavor of SQL which can complicate things.

hanniabu#
> And a few weeks/months later, we have forgotten much of it and have to relearn it all over again.

This has been exactly my situation. I love regex because it's so powerful but damn do I hate working with it due to the relearning process.

csunbird1 reply#
Please do not forget the fact that after a couple of months, you want to make a small change, but you forgot the edge cases when you first created the regex :D
b3kart2 replies#
Unit tests will help with this.

Continue this thread — 2 replies

m463#
I don't think regex is that specifically hard to learn, but you're fighting the escaping in your favorite language at the same time, so your learning is confounded.
logicallee1 reply#
>what is a literal character and what is a control character?

I read a tip back in the Perl 5 book, that you can just escape any character if you don't remember if it has a special meaning. (You'll still get the literal character even if it didn't need escaping.)
So I basically do that a lot. Never had any issue with control characters.

m4631 reply#
In my example, if you over-escape the first period or under-escape the second period the regex will undermatch or overmatch.

for me it took a few tries to get filexc to not match and file.c to match in my example in the 3 languages.

logicallee#
My comment was just about what you call "under-escap[ing] the second period."

I mean if you don't know if a ,;:"%$$@!_€|~ or any other character means something you can just write \ before that character. In other words, without thinking of whether it means something in your regex language. I don't think = means anything, but I would write \= to match an equals sign, so I don't even have to think about it. So my comment was about matching literal characters of any kind. I would have used a \ for the literal period out of habit, just because it wasn't [A-Za-z]. So my version would have been right the first time.

nobrains1 reply#
Hi, very nice RegEx educational site/book.

Feedback:
- In the chapter https://refrf.shreyasminocha.me/chapters/character-classes an example is given which uses:

  o ^ character outside brackets

  o $ end of line

  o +
But the explanation above does not introduce these yet, so a real beginner user (like me) is lost. The ambigious characters example is fine, since it uses all the concepts already explained.
shreyasminocha1 reply#
Thanks. Yeah, others pointed this out too. I'll get to it soon!
bathory#
Just wanted to chime in and also say that this is extremely confusing for a RegEx beginner, but thanks for your work, it looks really nice.
LeonB2 replies#
I like the example based approach. I learn from examples far quicker than I learn from “explanations”. If I attempt to learn from an example and my brain hits an exception, only then do I start reading the supporting text.

Nice approach. You’ve made a valuable thing and implemented a powerful idea.

kmundnic#
I've been using tldr [1] instead of man pages lately to get started with a command (or to remind myself how to use one). I've learned a lot just by reading the examples shown, and then read the man pages if I am missing something.

[1] https://github.com/tldr-pages/tldr

seph-reed#
Examples being greater than explanations is one of the main reasons I emphasize explanative error messaging, clear simple typings, and verbose function/variable names over documentation.

Docs are really good for discovery and should cover many topics shallowly so you can glean a big picture quickly. I generally don't like going to them for specs that could have just been an error message, a type, or a better naming convention.

wonnage2 replies#
I personally had the most trouble with regexes because I didn't have a good mental model of how they worked. The hard part wasn't finding the correct symbol/character class I was trying to match, but coming to grips with repetition, greedy/nongreedy, etc.

I took a compilers class in college where one of the projects was to implement a simple regex matcher using NFAs. Bashing my head against this for a week really helped with being able to "read" a regex. Not sure if this was due to finally understanding the algorithm, or the fact that I was just constantly staring at broken regex matches all day.
IMO it was a fairly small time investment for something that is so widely used.
I'll recommend this post that's been on HN many times: https://swtch.com/~rsc/regexp/regexp1.html

the-pigeon#
I'm not sure how to explain it but the most important thing I've learned in over a decade of programming experience is to not use regular expressions for many things they may seem like a regular expression problem.

For example, even something as simple a phone number can have all sorts of weird but valid variants. Be sure you really need to even validate it's format and not just that it's present.
Trying to handle all of those variants via regex expression is doable but a pain. And in practice you as the programmer should not be defining those variants that are valid as it's up to the business itself to define what type of data it considers to be valid for the field.
That said I've also worked for companies with small engineering teams where the goal has always been to be as efficient with development time as possible, as opposed to making a near ideal system. Software has different needs when it's used by a thousand people than when it's used by millions.

btilly#
I think it is understanding the algorithm that does it.

I also recommend that people learn how to read a regex by writing a small recursive program to match specific regexes. After you look at a regex and think about how it might work, intuition follows.
Actually writing the bit that turns the regex expression into said program isn't as important though. Doing that by hand 5 times is enough IMO.

Load more comments