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

Character sets in Regular Expressions (See related posts)

Here is a listing from my irb session, as you can see I'm learning character sets with regular expressions. The characters ('a' and 'e') within the square brackets are treated as operands which are XORed between the characters 'h' and 'y'.

irb(main):155:0> "hello"[/[ae]/]
=> "e"
irb(main):156:0> "hello"[/h[ae]y/]
=> nil
irb(main):157:0> "hay"[/h[ae]y/]
=> "hay"
irb(main):158:0> "howdy"[/h[ae]y/]
=> nil
irb(main):159:0> "hardy"[/h[ae]y/]
=> nil
irb(main):160:0> "hardy"[/h[ae]/]
=> "ha"
irb(main):161:0> "hardy"[/h[ae]y/]
=> nil
irb(main):162:0> "hoy"[/h[ae]y/]
=> nil
irb(main):165:0> "they"[/h[ae]y/]
=> "hey"
irb(main):166:0> "they would"[/h[ae]y/]
=> "hey"


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