Skip to content

Instantly share code, notes, and snippets.

@ruanwz
Created August 26, 2010 23:47
Show Gist options
  • Select an option

  • Save ruanwz/552488 to your computer and use it in GitHub Desktop.

Select an option

Save ruanwz/552488 to your computer and use it in GitHub Desktop.
reg2=/^[\w!#\$%&'*+\/=?`{|}~^-]+ #only allow to start with these symbols
(?:\.[\w!#\$%&'*+\/=?`{|}~^-]+)* # the user name can contain dot
@ # @ symbol
(?:[a-zA-Z0-9-]+\.)+ # domain
[a-zA-Z]{2,6}$ # the last part of domain
/x # ignore spaces
reg=/(?<allow_char> [\w!#\$%&'*+\/=?`{|}~^-]+){0} #only allow to start with these symbols
(?<script_tag> script){0}
^\g<allow_char>+
(?:\.\g<allow_char>)* # the user name can contain dot
@ # @ symbol
(?:[a-zA-Z0-9-]+\.)+ # domain
[a-zA-Z]{2,6}$ # the last part of domain
/x # ignore spaces
def encrypt str
str.gsub(/[A-Z]/) {|s| s.next}
end
puts 'jd@example.com'[reg] # => jd@example.com
puts '!jd@example.com'[reg] # => !jd@example.com
puts 'jd@example.tooolong'[reg] # => nil
puts 'j*d@example.com'[reg] # => j*d@example.com
puts 'john.doe@example.com.INFO'[reg] # => john.doe@example.com.INFO
puts 'jd.@example.com'[reg] # => nil
puts "<script>alert('boo');</script>\nevil@hacker.com"[reg] # => evil@hacker.com
puts encrypt "HAL"
reg_xml=/((?<tag> \<\w+\> \w*\g<tag>* \<\/\w+\>))/x
puts reg_xml =~ "<body></body>"
puts reg_xml =~ "<body><p>hi</p></body>"
puts reg_xml =~ "<body><p>hi</p><ul></ul></body>"
puts reg_xml =~ "<body><p>hi</p><ul><li>one</li><li>two</li></ul></body>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment