gpt4 book ai didi

ruby-on-rails - 用于多个单词的 ruby​​ 正则表达式有条件地匹配

转载 作者:数据小太阳 更新时间:2023-10-29 07:41:55 25 4
gpt4 key购买 nike

我想删除名称的所有前缀。 (例如,Prof.、Dr.、Mr. 等)可以在任何序列中超过一个。所以我想写一个正则表达式来 slice 所有这些前缀。我想在 ruby 中执行此操作。

以下是我要实现的输入/输出集。

"Prof. Dr. John Doe" => "John Doe"
"Dr. Prin. Gloria Smith" => "Gloria Smith"
"Dr. William" => "William"
"Sean Paul" => "Sean Paul"

我还想将删除的前缀存储在另一个字符串中。

"Prof. Dr. John Doe" => "Prof. Dr."
"Dr. Prin. Gloria Smith" => "Dr. Prin."
"Dr. William" => "Dr."
"Sean Paul" => ""

最佳答案

情况一:给定标题列表

假设

titles = ["Dr.", "Prof.", "Mr.", "Mrs.", "Ms.", "Her Worship", "The Grand Poobah"]

R = /
(?: # begin non-capture group
#{Regexp.union(titles)}
# "or" all the titles
\s* # match >= 0 spaces
)* # end non-capture group and perform >= 0 times
/x # free-spacing regex definition mode
#=> /
# (?: # begin non-capture group
# (?-mix:Dr\.|Prof\.|Mr\.|Mrs\.|Ms\.|Her\ Worship|The\ Grand\ Poobah)
# # "or" all the titles
# \s* # match >= 0 spaces
# )* # end non-capture group and perform >= 0 times
# /x

def extract_titles(str)
t = str[R] || ''
[str[t.size..-1], t.rstrip]
end

["Prof. Dr. John J. Doe, Jr.", "Dr. Prin. Gloria Smith", "The Grand Poobah Dr. No",
"Gloria Smith", "Cher, Ph.D."].each { |s| p extract_titles s }
# ["John J. Doe, Jr.", "Prof. Dr."]
# ["Prin. Gloria Smith", "Dr."]
# ["No", "The Grand Poobah Dr."]
# ["Gloria Smith", ""]
# ["Cher, Ph.D.", ""]

如果没有标题,如上两个例子,str[R] => nil,所以(str[R] || "").rstrip #=> "".rstrip #=> "".

请参阅类方法的文档 Regexp::union看看它是如何使用的。

情况二:没有标题列表

以下假定所有标题都是一个单词,以大写字母开头,后跟一个或多个小写字母,再后跟一个句点。如果不正确,可以相应地更改下面的正则表达式。

这种情况与前一种情况的唯一区别是正则表达式发生了变化。

R = /
\A # match beginning of string
(?: # start a non-capture group
[A-Z] # match a capital letter
[a-z]+ # match > 0 lower-case letters
\.\s* # match a period followed by >= 0 spaces
)* # end non-capture group and execute >= 0 times
/x # free-spacing regex definition mode

["Prof. Dr. John J. Doe, Jr.", "Dr.Prin.Gloria Smith",
"Gloria Smith", "Cher, Ph.D."].each { |s| p extract_titles(s) }
# ["John J. Doe, Jr.", "Prof. Dr."]
# ["Gloria Smith", "Dr. Prin."]
# ["Gloria Smith", ""]
# ["Cher, Ph.D.", ""]

关于ruby-on-rails - 用于多个单词的 ruby​​ 正则表达式有条件地匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36287306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com