Skip to content

Instantly share code, notes, and snippets.

@allolex
Last active August 29, 2015 14:00
Show Gist options
  • Save allolex/729138d8afaa267b6502 to your computer and use it in GitHub Desktop.
Save allolex/729138d8afaa267b6502 to your computer and use it in GitHub Desktop.

Revisions

  1. allolex revised this gist May 3, 2014. 6 changed files with 4 additions and 144 deletions.
    5 changes: 0 additions & 5 deletions 00_regular_expression_examples.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    # 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.
    4 changes: 4 additions & 0 deletions README.md
    Original 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
    61 changes: 0 additions & 61 deletions regular_expression_comments.rb
    Original file line number Diff line number Diff line change
    @@ -1,61 +0,0 @@
    #!/usr/bin/env ruby
    # Commenting regular expressions
    # Use the x flag after your regular expression

    re = /
    \A # the beginning of a string,
    < # followed by a '<'
    [^>]+ # followed by one or more non-'>' characters
    > # 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
    24 changes: 0 additions & 24 deletions regular_expression_escape.rb
    Original file line number Diff line number Diff line change
    @@ -1,24 +0,0 @@
    #!/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

    __END__
    They are the same, but one is more readable.
    /\[b\]/
    /\[b\]/
    7 changes: 0 additions & 7 deletions regular_expression_lookaround.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    #!/usr/bin/env ruby
    # Using look-ahead and look-behind assertions in
    # regular expressions

    # This is a stub

    __END__
    47 changes: 0 additions & 47 deletions regular_expression_union.rb
    Original file line number Diff line number Diff line change
    @@ -1,47 +0,0 @@
    #!/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 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.
  2. allolex revised this gist May 2, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions regular_expression_lookaround.rb
    Original 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__
  3. allolex revised this gist May 2, 2014. 1 changed file with 46 additions and 10 deletions.
    56 changes: 46 additions & 10 deletions regular_expression_comments.rb
    Original 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

    test_text = <<EOS
    <this-one-matches>
    <this-one-does-not-match>
    # 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 -->
    EOS
    END_OF_STRING

    test_text.split(/\n/).each do |line|
    message = if !! line.match(re)
    'SUCH MATCH!'
    else
    'MATCH FAIL :('
    end
    puts "#{message} '#{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
  4. allolex revised this gist May 2, 2014. 2 changed files with 18 additions and 1 deletion.
    5 changes: 5 additions & 0 deletions regular_expression_escape.rb
    Original 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\]/
    14 changes: 13 additions & 1 deletion regular_expression_union.rb
    Original file line number Diff line number Diff line change
    @@ -31,5 +31,17 @@
    puts "#{line}" if re === line
    end

    puts '','The regualar expression',re.inspect
    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.
  5. allolex renamed this gist May 2, 2014. 1 changed file with 0 additions and 0 deletions.
  6. allolex renamed this gist May 2, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. allolex revised this gist May 2, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions README.md
    Original 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.
  8. allolex revised this gist May 2, 2014. 1 changed file with 35 additions and 0 deletions.
    35 changes: 35 additions & 0 deletions regular_expression_union.rb
    Original 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.'
  9. allolex revised this gist May 2, 2014. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions regular_expression_escape.rb
    Original 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
  10. allolex revised this gist May 2, 2014. 1 changed file with 0 additions and 0 deletions.
    Empty file modified regular_expression_comments.rb
    100644 → 100755
    Empty file.
  11. allolex revised this gist Apr 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion regular_expression_comments.rb
    Original 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 one or more non-'>' characters
    > # followed by a '>'
    /x

  12. allolex created this gist Apr 15, 2014.
    25 changes: 25 additions & 0 deletions regular_expression_comments.rb
    Original 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