Skip to content

Instantly share code, notes, and snippets.

@taro-m
Created January 18, 2014 04:03
Show Gist options
  • Save taro-m/8486024 to your computer and use it in GitHub Desktop.
Save taro-m/8486024 to your computer and use it in GitHub Desktop.
Put `-` in regexp's char class

Conclusion

When writing a regex patter like [a-zA-Z0-9_-] which have - in char class, it is better that put - at top. [-a-zA-Z0-9_]

Why?

If - is put at last and after a character: [_-], As a parser of regexp pattern, when met - after _ (a literal character), it can't be determined as a range operator or a literal character soon. So parser pended it, and read next ] (end of char class), then treat previous - as a leteral character. It is not easy behavior.

But if - is put at top of char class, parser easily distiguish as a literal character.

Putting - at top of regexp's char class, is kindness for parser and its efficiency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment