Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Regarding your other point, characters like `*` won't need escaping within character class.

Really? Huh. I've been escaping them anyway, and I think I'll continue to do so just to avoid ambiguity when reading patterns.

I was about to end this post with "an extra backslash never hurt anybody" but then I realized that this is HN and writing that would surely prompt someone to reply with some bizarre case where an extra backslash caused a cascade of failures which cost a company fifteen million dollars and/or killed 43 people.



Obligatory example where the extra backslash makes a difference:

    $ printf '%s\n' 'asterisk: *' 'backslash: \' | grep '[*]'
    asterisk: *
    $ printf '%s\n' 'asterisk: *' 'backslash: \' | grep '[\*]'
    asterisk: *
    backslash: \
It depends on the regex flavour though (doesn't happen with Python, JavaScript, or Perl for example).


Haha, you are right to be skeptical. For example, `GNU grep` doesn't treat backslash as special within character class. You need to place metacharacters like `-`, `^`, `]`, etc in particular spots to match them literally.


Serious answer: some people use character classes as a more readable form of escape, e.g. [.]

So escaping inside would be extra redundant.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: