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

Repetition in Regular Expressions (See related posts)

This irb session example helps demonstrate repetition in regular expressions using * ? + {}

Definitions:

? - the preceding token is optional
+ - find the token 1 or more times
* - find the token 0 or more times
{n} - repeat the token n no of times


irb(main):058:0> "this song is my favourite"[/favou?rite/]
=> "favourite"
irb(main):059:0> "this song is my favorite"[/favou?rite/]
=> "favorite"

irb(main):100:0> "this is my favourite\s"[/this\s((song|video)\s)?is/]
=> "this is"
irb(main):101:0> "this song is my favourite\s"[/this\s((song|video)\s)?is/]
=> "this song is"
irb(main):102:0> "this flower is my favourite\s"[/this\s((song|video)\s)?is/]
=> nil
irb(main):103:0> "this video is my favourite\s"[/this\s((song|video)\s)?is/]
=> "this video is"

irb(main):105:0> "anything you say"[/.*/]
=> "anything you say"
irb(main):119:0> "will be repeated"[/.*/]
=> "will be repeated"
irb(main):116:0> "good times</p>"[/.*[^<\/p>]/]
=> "good times"

irb(main):220:0> "test 123 test 123"[/\d+/]
=> "123"
irb(main):224:0> "test 123 test 123"[/\d{2}/]
=> "12"
irb(main):226:0> "test 123 test 123"[/\w+/]
=> "test"

irb(main):008:0> "try this test that"[/try.\b\w+/]
=> "try this"
irb(main):009:0> "try this test that"[/test.\b\w+/]
=> "test that"

irb(main):233:0> "test 123 test 123"[/\w+\s\d+/]
=> "test 123"
irb(main):248:0> "test 123 test 123"[/\w+\s\d+.*/]
=> "test 123 test 123"
irb(main):249:0> "test this test 123"[/\w+\s\d+.*/]
=> "test 123"
irb(main):250:0> "test this test that"[/\w+\s\d+.*/]
=> nil
irb(main):279:0> "try this test that"[/try\s\w[^\s]+/]
=> "try this"
irb(main):280:0> "try this test that"[/test\s\w[^\s]+/]
=> "test that"

irb(main):310:0> "try this test that"[/test\s\w\B+/]
=> "test tha"
irb(main):308:0> "try this test that"[/try\s\w\B+./]
=> "try this"
irb(main):309:0> "try this test that"[/test\s\w\B+./]
=> "test that"

irb(main):322:0> "try this test that"[/try\s\w+\b/]
=> "try this"
irb(main):323:0> "try this test that"[/test\s\w+\b/]
=> "test that"

irb(main):008:0> "try this test that"[/try.\b\w+/]
=> "try this"
irb(main):009:0> "try this test that"[/test.\b\w+/]
=> "test that"

irb(main):010:0> "try this test that"[/test.\w+/]
=> "test that"
irb(main):011:0> "try this test that"[/try.\w+/]
=> "try this"

irb(main):012:0> "try this test that"[/try\s\w+/]
=> "try this"
irb(main):013:0> "try this test that"[/test\s\w+/]
=> "test that"

irb(main):015:0> "try this test that"[/try\s\w+\s\w+/]
=> "try this test"

irb(main):041:0> "try this test that"[/\w+\s\w+$/]
=> "test that"
irb(main):043:0> "try this test that"[/^\w+\s\w+/]
=> "try this"



Note: Similar to CSS the more specific an expression or selector is the less ambiguous it is.

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


Click here to browse all 4861 code snippets

Related Posts