Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Shorthand Character Classes in Regular Expressions (See related posts)

Learning Regular Expressions can seem tedious however with a little knowledge you can programmatically select or validate specific text in a few lines of code. key: \d = digit; \w = alphanumeric; \s space (includes tabs and line-breaks)

irb(main):462:0> "happy"[/\d/]
=> nil
irb(main):463:0> "happy"[/\w/]
=> "h"
irb(main):464:0> "123 happy"[/\w/]
=> "1"
irb(main):465:0> "123 happy"[/\d/]
=> "1"
irb(main):466:0> "123 happy"[/\d\s/]
=> "3 "
irb(main):467:0> "123 happy"[/\d{3}/]
=> "123"
irb(main):468:0> "123 happy"[/\d{3}\s\w{3}/]
=> "123 hap"


You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts