3

我有一个主题标题包含在“|”中的文档 人物。

例如“|链接|”

我想检查字符串是否以“|”开头和结尾 用于验证特定文档的主题标题是否有效的字符。我该怎么做?

来源:

@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
4

2 回答 2

19

你试过RDoc吗?

"| LINKS |".start_with?("|") # => true
"| LINKS |".end_with?("|") # => true
于 2011-06-18T08:09:37.303 回答
6

您可以只使用一个简单的正则表达式:

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

这样您就可以验证其他内容,例如标题必须全部大写并且需要在其周围留出空格 ( /^\|\s[A-Z]+\s\|$/)。

于 2011-06-18T08:42:35.417 回答