Last active August 29, 2015 14:00
-
-
Save allolex/729138d8afaa267b6502 to your computer and use it in GitHub Desktop.
Revisions
-
allolex revised this gist
May 3, 2014 . 6 changed files with 4 additions and 144 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ I have moved the regular expression example repository to the regular GitHub site to encourage contributions. https://github.com/allolex/ruby-regular-expressions This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,61 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,24 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,47 +0,0 @@ -
allolex revised this gist
May 2, 2014 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ #!/usr/bin/env ruby # Using look-ahead and look-behind assertions in # regular expressions # This is a stub __END__ -
allolex revised this gist
May 2, 2014 . 1 changed file with 46 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,6 @@ #!/usr/bin/env ruby # Commenting regular expressions # Use the x flag after your regular expression re = / \A # the beginning of a string, @@ -8,18 +9,53 @@ > # followed by a '>' /x # Second method for compiling the regex # I'm using a heredoc here with `<<` re_two_string = <<-'END_OF_REGEX' \A # beginning of a string \s+ # one or more spaces < # a '<' character END_OF_REGEX # Regexp::EXTENDED is the same as the `x` flag re_two = Regexp.new(re_two_string, Regexp::EXTENDED) test_text = <<'END_OF_STRING' <this-one-matches-the-first-re> <this-one-matches-the-second-re> <match> <> <!-- no match --> END_OF_STRING test_text.split(/\n/).each do |line| message = if re === line 'SUCH MATCH on the first RE !' elsif re_two === line 'SUCH MATCH on the second RE !' else 'MATCH FAIL :(' end puts "#{message} --> '#{line}'" end puts '',re.inspect puts '',re_two.inspect __END__ SUCH MATCH on the first RE ! --> '<this-one-matches-the-first-re>' SUCH MATCH on the second RE ! --> ' <this-one-matches-the-second-re>' SUCH MATCH on the first RE ! --> '<match>' MATCH FAIL :( --> '<> <!-- no match -->' / \A # the beginning of a string, < # followed by a '<' [^>]+ # followed by one or more non-'>' characters > # followed by a '>' /x / \A # beginning of a string \s+ # one or more spaces < # a '<' character /x -
allolex revised this gist
May 2, 2014 . 2 changed files with 18 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,3 +17,8 @@ puts bbcode_bold_re_one.inspect puts bbcode_bold_re_two.inspect __END__ They are the same, but one is more readable. /\[b\]/ /\[b\]/ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,5 +31,17 @@ puts "#{line}" if re === line end puts '','The regular expression',re.inspect puts '','As you can see, it preserves any regex flags you include.' __END__ <head> </head> <body> <p>Some text.</p> </body> The regular expression /(?i-mx:(?<head><[\/]?head>))|(?-mix:(?<body><[\/]?body>))|(?-mix:(?<paragraph><[\/]?p>))/ As you can see, it preserves any regex flags you include. -
allolex renamed this gist
May 2, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
allolex renamed this gist
May 2, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
allolex revised this gist
May 2, 2014 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ # Regular expression examples So that Rubyists' regex-fu can be improved, as someone who has used regular expressions for a long time, I would like to provide examples of regexes in Ruby. -
allolex revised this gist
May 2, 2014 . 1 changed file with 35 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ #!/usr/bin/env ruby # Combining regular expressions with Regexp.union # Avdi Grimm has covered this in his Ruby Tapas screencast (paid) # http://www.rubytapas.com/episodes/199-Regexp-Union head_re = /(?<head><[\/]?head>)/i body_re = /(?<body><[\/]?body>)/ paragraph_re = /(?<paragraph><[\/]?p>)/ test_text = <<EOP <head> <title>This</title> <meta></meta> </head> <body> <div>Unprinted text.<div> <p>Some text.</p> </body> EOP re = Regexp.union( head_re, body_re, paragraph_re ) test_text.each_line do |line| line.chomp # The === operator returns true if the string on the # right matches the regular expression on the left puts "#{line}" if re === line end puts '','The regualar expression',re.inspect puts '','As you can see, it preserves any regex flags you include.' -
allolex revised this gist
May 2, 2014 . 1 changed file with 19 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ #!/usr/bin/env ruby # Escaping text in regular expressions # Regexp.escape is the same as Regexp.quote # The example uses a BBCode tag, which was one of the # simplest things I could come up with. bbcode_bold_re_one = /\[b\]/ escaped_text = Regexp.escape('[b]') bbcode_bold_re_two = Regexp.new(escaped_text) if bbcode_bold_re_one == bbcode_bold_re_two puts 'They are the same, but one is more readable.' else puts 'They are different.' end puts bbcode_bold_re_one.inspect puts bbcode_bold_re_two.inspect -
allolex revised this gist
May 2, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
Empty file. -
allolex revised this gist
Apr 15, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ re = / \A # the beginning of a string, < # followed by a '<' [^>]+ # followed by one or more non-'>' characters > # followed by a '>' /x -
allolex created this gist
Apr 15, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ #!/usr/bin/env ruby # Commenting regular expressions re = / \A # the beginning of a string, < # followed by a '<' ([^>]+) # followed by one or more non-'>' characters, > # followed by a '>' /x test_text = <<EOS <this-one-matches> <this-one-does-not-match> <match> <> <!-- no match --> EOS test_text.split(/\n/).each do |line| message = if !! line.match(re) 'SUCH MATCH!' else 'MATCH FAIL :(' end puts "#{message} '#{line}'" end