美文网首页Ruby & Rails
ruby 中的正则表达式

ruby 中的正则表达式

作者: 云莉6 | 来源:发表于2018-09-28 17:47 被阅读0次

摘录几个 ruby 正则表达式

# bad
hex_pattern = \
  /^(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)+$/
  
# Valid matches
"123abd" =~ hex_pattern #=> 0
"456789" =~ hex_pattern #=> 0

# Invalid, not hex characters
"876jkl" =~ hex_pattern #=> nil
  
# good
hex_pattern = /^[0-9a-f]+$/
message = <<-MESSAGE
The original idea came from @r00k,
then @christoomey did a first pass,
and @jferris came through to help
clean up the implementation.
MESSAGE

username_pattern = /@\w+/

message.scan(username_pattern)
#=> ["@r00k", "@christoomey", "@jferris"]
string = "The %start% and %end% of the content..."

# .*
string.scan(/%.*%/)
#=> ["%start% and %end%"]

# .*? 
string.scan(/%.*?%/)
#=> ["%start%", "%end%"]
# bad
/https:\/\/.*?\/api\/(.*?)\/(.*?)/

# good
%r{https://.*?/api/(.*?)/(.*?)}
# bad
/[0-9]+[ \t\r\n\f][a-zA-Z0-9_]*/

# good
/\d+\s\w*/
.     Any character except a newline
\w  A word character ([a-zA-Z0-9_])
\W  A non-word character ([^a-zA-Z0-9_])
\d  A digit character ([0-9])
\D  A non-digit character ([^0-9])
\h  A hex digit character ([0-9a-fA-F])
\H  A non-hex digit character ([^0-9a-fA-F])
\s  A whitespace character: ([ \t\r\n\f])
\S  A non-whitespace character: ([^ \t\r\n\f])
string = <<-STRING
The word class is a reserved word,
but we can use other words that
contain it, like "class_name", or
"klass" to work around this.
STRING

pattern = /\b[ck]lass\b/

string.scan(pattern)
#=> ["class", "klass"]
sample = \
  "{ count: 2, title: 'Hello', section: 3 }"

sample.scan(/\w+(?=:)/)
#=> ["count", "title", "section"]
valid_email = /.+@.+\..+/

possible_emails = [
  "user@example.com",
  "person@thing",
  "ben@upcase.com",
  "user@place.it",
  "asdf"
]

possible_emails.grep(valid_email)
#=> [
#     "user@example.com",
#     "ben@upcase.com",
#     "user@place.it"
#   ]

# Array#grep is essentially a shorthand for:
possible_emails.filter do |email|
  email =~ pattern
end
# 把句子中的 10/8/14 变成 Monday Aug 8
require "date"

sample = <<-SAMPLE
The first battle was on 10/8/14,
followed by additional skirmishes
on 11/9/14 and 11/12/14.
SAMPLE

date_pattern = %r{\d+/\d+/\d+}

sample.gsub(date_pattern) do |date|
  Date.
    strptime(date, "%m/%d/%y").
    strftime("%A %b %-d")
end

You Can't Parse HTML With Regex.

新添:

# 去空格及一些特殊字符
str.gsub(/\s|\\|\'|\/|\?/, "") if params[:q].present?

# 仅拿出字符串中的数字及大小写字母
str.gsub(/[^0-9A-Za-z]/, "")

相关文章

  • 转:Ruby笔记–正则表达式

    Ruby笔记–正则表达式 Ruby对正则表达式支持非常好,下面将对我经常使用到的做一个总结,包括Ruby中正则的写...

  • ruby 中的正则表达式

    摘录几个 ruby 正则表达式 You Can't Parse HTML With Regex. 新添:

  • Ruby 正则表达式

    模式与匹配 创建正则表达式对象的语法如下: 例如, 匹配"Ruby"这个字符串的正则表达式为: 把希望匹配的内容直...

  • Effective Ruby

    一、 让自己熟悉Ruby 1、理解 Ruby 中的 True 在 Ruby 中,除了 false 和 nil, 其...

  • Ruby安装(Mac)

    Ruby安装 ruby-install 和 chruby 管理 Ruby ruby-install能在任意目录中编...

  • CentOS 7 配置Ruby语言开发环境

    CentOS 7 配置Ruby语言开发环境 Ruby环境 安装Ruby 2.2 CentOS7存储库中的Ruby版...

  • 工作优化

    1.写代码中的打印花点心思,使其容易发现问题。 2.熟悉Ruby对目录和文件的操作,以及各种正则表达式。和操作Ex...

  • ruby的正则表达式

    http://www.w3cschool.cc/ruby/ruby-regular-expressions.htm...

  • iOS Mac安装CocoaPods

    1、查看ruby版本,在终端中输入ruby -v,2.0版本过低,需要升级ruby 2、升级ruby 1)检查是否...

  • RubyGems、Gem、Bundle

    类比于C#中的包,Java中的package,Ruby程序中的代码库( ruby software package...

网友评论

    本文标题:ruby 中的正则表达式

    本文链接:https://www.haomeiwen.com/subject/ksgpoftx.html