ruby start_with?(),end_with?()

|| 我有一个主题标头用\“ | \”字符括起来的文档。 例如。 \“ |链接| \” 我想检查字符串是否以\“ | \”字符开头和结尾,以验证特定文档的主题标头是否有效。我该怎么做? 资源:
@filetarget = \" < document file location here > \"

@line = \"\"

file = File.new(@filetarget)

while (@line = file.gets)
   if((@line.start_with?(\"|\")) and (@line.end_with?(\"|\")))
      puts \"Putting: \" + @line
   end
end
用于解析的文档文字:
| 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
    
已邀请:

bab

        您可以只使用一个简单的正则表达式:
if line =~ /^\\|.*\\|$/
这样,您可以验证其他内容,例如标头必须全部为大写且在其周围需要空格(
/^\\|\\s[A-Z]+\\s\\|$/
)。     
        您尝试过RDoc吗?
\"| LINKS |\".start_with?(\"|\") # => true
\"| LINKS |\".end_with?(\"|\") # => true
    

要回复问题请先登录注册