试图检查一行是否在开头包含某些内容并且其中包含某个单词

[英]Trying to check to see if a line contains something in the start and has a certain word in it


returns 1 if the file contains the string "attach" on a line that doesn't start with >. Return 0 otherwise. A script's return value is set as the parameter to Ruby's exit function. After running the script, echo $? to check the returned value.

如果文件在不以>开头的行上包含字符串“attach”,则返回1。否则返回0。脚本的返回值被设置为Ruby的退出函数的参数。运行脚本后,回显$?检查返回的值。

I'm working on this problem and have a basic understanding of what must be done. I need to first check if the line starts with >, then search for the exact word "attach", but I don't know how to actually code this in ruby and need some help. I feel dumb because I'm sure this is a simple thing to do and I'm stumped.

我正在研究这个问题,并对必须做的事情有了基本的了解。我需要首先检查行是否以>开头,然后搜索确切的单词“attach”,但我不知道如何在ruby中实际编写代码并需要一些帮助。我觉得愚蠢,因为我确信这是一件很简单的事,我很难过。

Thank you so much for any help.

非常感谢你的帮助。

Edit: you wanted to see what i had so far for code and it's not much but... I know if i use something like ^> I can see if it starts with the > and I can just spell out "attach" to get that meaning I could use something ALMOST like ^> attach except i need the negation of ^>, which I'm not sure how to do. I tried messing around with different spots of putting a ! to negate it, but didn't get any luck with that.

编辑:你想看看到目前为止我的代码是什么,它并不多但是......我知道如果我使用像^>我可以看到它是否以>开头我可以拼出“附加”来获取这意味着我可以使用几乎像^>附加的东西,除了我需要否定^>,我不知道该怎么做。我试着把不同的点放在一起!否定它,但没有得到任何运气。

I also know that I can't just have it like that but I need something like (?'attach'), but am not fully sure what is the best way to do that.

我也知道我不能只是这样,但我需要像(?'attach')这样的东西,但我不完全确定最好的方法是什么。

3 个解决方案

#1


0  

Check out the ruby String class: http://www.ruby-doc.org/core-2.1.1/String.html. Note the start_with?() method and the match() method. You don't need a complex regex, you can use a compound if statement:

查看ruby String类:http://www.ruby-doc.org/core-2.1.1/String.html。注意start_with?()方法和match()方法。您不需要复杂的正则表达式,可以使用复合if语句:

line = "abc attach"

if (not line.start_with?('>')) and line.match("attach")
  puts 'yes'
else 
  puts 'no'
end

--output:--
yes

to get that meaning I could use something ALMOST like ^> attach except i need the negation of ^>, which I'm not sure how to do.

为了得到那个含义,我可以使用像^> attach这样的东西,除了我需要否定^>,我不知道该怎么办。

You can use a character class: [>], which matches '>', and then negate the class: [^>], which matches anything but a '>' character.

您可以使用匹配'>'的字符类:[>],然后取消类:[^>],它匹配除“>”字符之外的任何内容。

also know that I can't just have it like that but I need something like (?'attach'),

也知道我不能只是这样,但我需要像(?'attach'),

If you have to use a regex, then think of wording the regex like this:

如果你必须使用正则表达式,那么考虑像这样的正则表达式的措辞:

1) You want to match the start of the string
2) Followed by any character that is not a '>' 
3) Followed by zero or more of any character  (see the regex character . and the modifier *)
4) Followed by the word 'attach'

#2


0  

Rubular: a Ruby regular expression editor and tester

Rubular:一个Ruby正则表达式编辑器和测试器

http://rubular.com/

http://rubular.com/

This should get you started with regular expressions.

这应该让你开始使用正则表达式。

#3


0  

If you want to match any non-">" character in a regex, you can use the negation symbol "^" within a custom character class (all characters within a set of square brackets [])

如果要匹配正则表达式中的任何非“>”字符,可以在自定义字符类中使用否定符号“^”(方括号[]中的所有字符)

1.9.3-p448 :038 > one = "> line with"
 => "> line with"
1.9.3-p448 :039 > two = ">attach"
 => ">attach"
1.9.3-p448 :040 > three = "$#@asdfj>dsfkjhattach"
 => "$>dsfkjhattach"
1.9.3-p448 :041 > /^[^>].*attach/.match(one)
 => nil
1.9.3-p448 :042 > /^[^>].*attach/.match(two)
 => nil
1.9.3-p448 :043 > /^[^>].*attach/.match(three)
 => #<MatchData "$>dsfkjhattach">

So, a method that does what you are asking for would be:

因此,满足您要求的方法是:

def matchAttachNoAngleBracket(subject)
  /^[^>].*attach/.match(subject) ? "yes" : "no"
end
1.9.3-p448 :047 > matchAttachNoAngleBracket(one)
 => "no"
1.9.3-p448 :048 > matchAttachNoAngleBracket(two)
 => "no"
1.9.3-p448 :049 > matchAttachNoAngleBracket(three)
 => "yes"

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/05/07/bc1a4aec8ea1a3c931a3e010c1207c6c.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号