Ruby 2.5、根据子串获取字符串的第一部分
Ruby 2.5, get first part of string depending substring
在 Ruby 中获取字符串的第一部分并删除第二部分的最佳方法是什么?
我知道每个案例要保留的第一部分
示例:
"The Cuvée titi" => "The Cuvée"
"The Cuvée toto" => "The Cuvée"
"The Cuvée toto 1234" => "The Cuvée"
"1234 The Cuvée" => need to do nothing
"The wine 45 67" => "The wine"
"The wine not good" => " The wine"
"What's The wine ?" => need to do nothing
在阅读了一些讨论后,我尝试了很多东西,包括:
- sub(), delete() => 保留第二部分,而不是第一部分
- 甚至 ruby2.5 的 delete_suffix() => 在某些情况下不工作
- string[0..x] => 但当字符串的开头不好时也可以工作
是否需要正则表达式?
谢谢
好吧,这就是我所做的:
if string.start_with? string_to_keep
new_string = string[0.. string_to_keep.length-1]
end
当然不是最好的 Ruby 方法,但是工作...
key_string = "The Cuvée"
example_string_1 = "The Cuvée toto 1234"
example_string_2 = "1234 The Cuvée"
index
will return the first occurrence of the key string in your examples. You can then return everything including the first occurrence of your key string with slice
,喜欢:
result1 = example_string_1.slice(0, example_string_1.index(key_string) + key_string.length)
=> "The Cuvée"
result2 = example_string_2.slice(0, example_string_2.index(key_string) + key_string.length)
=> "1234 The Cuvée"
我不太明白这个问题
如果你知道字符串的第一部分是什么,而你只想保留字符串的第一部分,则不需要做任何事情,因为你已经知道字符串的第一部分是什么.
if string.start_with?(string_to_keep)
string_to_keep
end
在我看来,objective 可以被视为删除给定字符串后面的字符串部分。
def doit(str, given_str)
str.gsub(/(?<=\b#{given_str}\b).*/, '')
end
given_str = "The Cuvée"
doit "The Cuvée titi", given_str #=> "The Cuvée"
doit "The Cuvée toto", given_str #=> "The Cuvée"
doit "The Cuvée toto 1234", given_str #=> "The Cuvée"
doit "1234 The Cuvée", given_str #=> "1234 The Cuvée"
given_str = "The wine"
doit "The wine 45 67", given_str #=> "The wine"
doit "The wine not good", given_str #=> "The wine"
doit "What's The wine?", given_str #=> "What's The wine"
请注意,最后一个示例中的问号已被删除。
对于
given_str = "The wine"
正则表达式是
/(?<=\b#{given_str}\b).*/
#=> /(?<=\bThe wine\b).*/
(?<=\bThe wine\b)
是 正后视 ,这意味着匹配前面有 "The wine"
,两边都有分词符。 .*
匹配零个或多个字符,尽可能多,即字符串中剩余的字符。
given_str = "The wine"
的另一个例子:
doit "Go to The winery for The wine", given_str
#=> "Go to The winery for The wine"
没有分词 (\b
) 这将是:
doit "Go to The winery for The wine", given_str
#=> "Go to The wine"
问题没有告诉我想要哪个结果。
在 Ruby 中获取字符串的第一部分并删除第二部分的最佳方法是什么? 我知道每个案例要保留的第一部分
示例:
"The Cuvée titi" => "The Cuvée"
"The Cuvée toto" => "The Cuvée"
"The Cuvée toto 1234" => "The Cuvée"
"1234 The Cuvée" => need to do nothing
"The wine 45 67" => "The wine"
"The wine not good" => " The wine"
"What's The wine ?" => need to do nothing
在阅读了一些讨论后,我尝试了很多东西,包括:
- sub(), delete() => 保留第二部分,而不是第一部分
- 甚至 ruby2.5 的 delete_suffix() => 在某些情况下不工作
- string[0..x] => 但当字符串的开头不好时也可以工作
是否需要正则表达式? 谢谢
好吧,这就是我所做的:
if string.start_with? string_to_keep
new_string = string[0.. string_to_keep.length-1]
end
当然不是最好的 Ruby 方法,但是工作...
key_string = "The Cuvée"
example_string_1 = "The Cuvée toto 1234"
example_string_2 = "1234 The Cuvée"
index
will return the first occurrence of the key string in your examples. You can then return everything including the first occurrence of your key string with slice
,喜欢:
result1 = example_string_1.slice(0, example_string_1.index(key_string) + key_string.length)
=> "The Cuvée"
result2 = example_string_2.slice(0, example_string_2.index(key_string) + key_string.length)
=> "1234 The Cuvée"
我不太明白这个问题
如果你知道字符串的第一部分是什么,而你只想保留字符串的第一部分,则不需要做任何事情,因为你已经知道字符串的第一部分是什么.
if string.start_with?(string_to_keep)
string_to_keep
end
在我看来,objective 可以被视为删除给定字符串后面的字符串部分。
def doit(str, given_str)
str.gsub(/(?<=\b#{given_str}\b).*/, '')
end
given_str = "The Cuvée"
doit "The Cuvée titi", given_str #=> "The Cuvée"
doit "The Cuvée toto", given_str #=> "The Cuvée"
doit "The Cuvée toto 1234", given_str #=> "The Cuvée"
doit "1234 The Cuvée", given_str #=> "1234 The Cuvée"
given_str = "The wine"
doit "The wine 45 67", given_str #=> "The wine"
doit "The wine not good", given_str #=> "The wine"
doit "What's The wine?", given_str #=> "What's The wine"
请注意,最后一个示例中的问号已被删除。
对于
given_str = "The wine"
正则表达式是
/(?<=\b#{given_str}\b).*/
#=> /(?<=\bThe wine\b).*/
(?<=\bThe wine\b)
是 正后视 ,这意味着匹配前面有 "The wine"
,两边都有分词符。 .*
匹配零个或多个字符,尽可能多,即字符串中剩余的字符。
given_str = "The wine"
的另一个例子:
doit "Go to The winery for The wine", given_str
#=> "Go to The winery for The wine"
没有分词 (\b
) 这将是:
doit "Go to The winery for The wine", given_str
#=> "Go to The wine"
问题没有告诉我想要哪个结果。