开发者

ruby start_with?(), end_with?()

开发者 https://www.devze.com 2023-03-14 06:07 出处:网络
I have a document with subject headers being enclosed in \"|\" characters. E.g. \"| LINKS |\" I want to check the string if it begins and ends with \"|\" character to verify that the particular docu

I have a document with subject headers being enclosed in "|" characters.

E.g. "| LINKS |"

I want to check the string if it begins and ends with "|" character to verify that the particular document's subject header is valid. How do I do so ?

Source:

@filetarget = " < document file location here > "

@line = ""

file = File.new(@filetarget)

while (@line = file.ge开发者_如何学Cts)
   if((@line.start_with?("|")) and (@line.end_with?("|")))
      puts "Putting: " + @line
   end
end

Document text for parsing:

| LINKS |

http://www.extremeprogramming.org/  <1>

http://c2.com/cgi/wiki?ExtremeProgramming  <2>

http://xprogramming.com/index.php  <3>

| COMMENTS |

* Test comment
* Test comment 2
* Test comment 3


Did you try the RDoc?

"| LINKS |".start_with?("|") # => true
"| LINKS |".end_with?("|") # => true


You can just use a simple regular expression:

if line =~ /^\|.*\|$/

That way you can verify other things, like that the header has to be all caps and needs spaces around it (/^\|\s[A-Z]+\s\|$/).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号