简体   繁体   English

如何使用Ruby / Rails在字符串中查找特定单词

[英]How to find a specific word in string with Ruby/Rails

I got a few string like so: 我得到一些像这样的字符串:

TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk

AND

TFjyg9780878_867978-DGB097908-78679iuhi698_sky_light_87689uiyhk

AND

TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_dark_87689uiyhk

AND

TFjyg9780878_867978-DGB097908-78679iuhi698_sky_dark_87689uiyhk

I need to check whether the strings above has one of the widesky_light , sky_light , widesky_dark and sky_dark with exactitude so I wrote this: 我需要检查上面的字符串是否具有widesky_light sky_lightwidesky_lightsky_lightwidesky_darksky_dark ,所以我这样写:

if my_string.match("widesky_light")
  ...
end 

For each variant, but the problem I'm having is because sky_light and widesky_light are similar, my code is not working properly. 对于每个变体,但我遇到的问题是因为sky_lightwidesky_light相似,所以我的代码无法正常工作。 I believe the solution to the above would be a regex, but I've spend the afternoon yesterday trying to get it to work without much success. 我相信上述解决方案将是一个正则表达式,但是昨天我花了整个下午试图使它正常工作。

Any suggestions? 有什么建议么?

EDIT 编辑

A caveat: in this string (as example): TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk , the part after widesky_light , which is _87689uiyhk is optional, meaning that sometimes I have it, sometimes I don't, so a solution would not be able to count on _string_ . 一个警告:在此字符串(为例): TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk ,之后的部分widesky_light ,这是_87689uiyhk是可选的,这意味着有时我有它,有时我不知道,这样的解决方案将无法计数在_string_

Looks like you just need to reorder your if statements 看起来您只需要对if语句重新排序

if my_string.match(/widesky_light/)
  return 'something'
end
if my_string.match(/sky_light/)
  return 'something'
end

Maybe something like this: 也许是这样的:

case my_string
when /_(sky_light)/
  # just sky_light
when /sky_light/
  # widesky_light
when /_(sky_dark)/
  # just sky_dark
when /sky_dark/
  # widesky_dark
else
  puts "I don't like"
end

Regex 正则表达式

1st regex : extract word for further checking 第一个正则表达式:提取单词以进行进一步检查

Here's a regex which only matches the interesting part : 这是一个仅与有趣部分匹配的正则表达式:

(?<=_)[a-z_]+(?=(?:_|\b))

It means lowercase word with possible underscore inside, between 2 underscores or after 1 underscore and before a word boundary . 它表示lowercase word with possible underscore inside, between 2 underscores or after 1 underscore and before a word boundary If you need some logic depending on the case (widesky, sky, light or dark), you could use this solution. 如果根据情况需要一些逻辑(宽天空,天空,亮或暗),则可以使用此解决方案。

Here in action. 在这里行动。

2nd regex : direct check if one of 4 words is present 第二个正则表达式:直接检查是否存在4个单词之一

If you just want to know if any of the 4 cases is present : 如果您仅想了解这4种情况中的任何一种:

(?<=_)(?:wide)?sky_(?:dark|light)(?=(?:_|\b))

Here in action, with either _something_after or nothing. 这里的行动,无论是_something_after或没有。

Case statement 案例陈述

list = %w(
  TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_light_87689uiyhk
  TFjyg9780878_867978-DGB097908-78679iuhi698_sky_light_87689uiyhk
  TFjyg9780878_867978-DGB097908-78679iuhi698_widesky_dark_87689uiyhk
  TFjyg9780878_867978-DGB097908-78679iuhi698_sky_dark_87689uiyhk
  TFjyg9780878_867978-DGB097908-78679iuhi698_trash_dark_87689uiyhk
)

list.each do |string|
  case string
  when /widesky_light/ then puts "widesky light found!"
  when /sky_light/     then puts "sky light found!"
  when /widesky_dark/  then puts "widesky dark found!"
  when /sky_dark/      then puts "sky dark found!"
  else                      puts "Nothing found!"
  end
end

In this order, the case statement should be fine. 按照这种顺序,case语句应该没问题。 widesky_dark won't match twice, for example. 例如, widesky_dark不会匹配两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM