红宝石开始_with?(),结束_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
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.gets)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试过 RDoc 吗?
Did you try the RDoc?
您可以只使用一个简单的正则表达式:
这样您就可以验证其他内容,例如标头必须全部大写并且周围需要空格(
/^\|\s[AZ]+\s\|$ /)。
You can just use a simple regular expression:
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\|$/
).