4 ms·
> 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 con
by Cyberdog 4y ago
> 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.
- asicsp 4y agoHaha, 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.
- hddqsb 4y agoObligatory 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).
- deleted 4y ago[deleted]
- xdfgh1112 4y agoSerious answer: some people use character classes as a more readable form of escape, e.g. [.] So escaping inside would be extra redundant.