如何在给定位置获取文件中的单词
def get_word(file, position)
File.each_line(file).with_index do |line, line_number|
if (line_number + 1) == position.line_number
# How to get a word at position.column_number ?
end
end
end
这应该像这样工作:
文件:message.md
Dear people:
My name is [Ángeliño](#angelino).
Bye!
呼叫:get_word
record Position, line_number : Int32, column_number : Int32
get_word("message.md", Position.new(1, 9)) # => people
get_word("message.md", Position.new(3, 20)) # => Ángeliño
get_word("message.md", Position.new(5, 3)) # => Bye!
也许,这会给你一个提示。请注意,此实现永远不会将标点符号视为单词的一部分,因此最后一个示例返回Bye
而不是Bye!
。
def get_word_of(line : String, at position : Int)
chunks = line.split(/(p{P}|p{Z})/)
edge = 0
hashes = chunks.map do |chunk|
next if chunk.empty?
{chunk => (edge + 1)..(edge += chunk.size)}
end.compact
candidate = hashes.find { |hash| hash.first_value.covers?(position) }
.try &.first_key
candidate unless (candidate =~ /A(?:p{P}|p{Z})+Z/)
end
p get_word_of("Dear people:", 9) # => people
p get_word_of("My name is [Ángeliño](#angelino).", 20) # => Ángeliño
p get_word_of("Bye!", 3) # => Bye
获取字符串部分的数字位置的一种便捷方法是使用如下所示的正则表达式:
filename = "/path/to/file"
File.each_line(filename).each_with_index do |line, line_number|
term = "search-term"
column = line =~ %r{#{term}}
p "Found #{term} at line #{line_number}, column #{column}." if column
end
输出:"Found search-term at line 38, column 6"