Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用预清理的索引为字符串编制索引?_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby 如何使用预清理的索引为字符串编制索引?

Ruby 如何使用预清理的索引为字符串编制索引?,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,我有一个字符串定义,其中可以显示HTML,还有一个单词数组。我试图在定义中搜索这些单词,并返回开始和结束位置。例如,我可能想在以下位置找到“Hello”: definition = "<strong>Hel</strong>lo World!" 将返回0。我需要起点为8,终点为21。我考虑将整个字符串映射到我自己的索引,如 {"1" => '<', "2" => 's', "3" => 't', .. , "9" => 'H' ...}

我有一个字符串
定义
,其中可以显示HTML,还有一个单词数组。我试图在
定义中搜索这些单词,并返回开始和结束位置。例如,我可能想在以下位置找到
“Hello”

definition = "<strong>Hel</strong>lo World!"
将返回
0
。我需要起点为
8
,终点为
21
。我考虑将整个字符串映射到我自己的索引,如

{"1" => '<', "2" => 's', "3" => 't', .. , "9" => 'H' ...}

{“1”=>”我承认我对HTML了解不多。我假设目标单词的每一对相邻字母(这里是“Hello”)都由零个或多个字符串分隔,这些字符串由
括起来,没有其他内容(但不知道这是否正确)

def doit(str,word)
r=Regexp.new(word.chars.join('(?:)*'))
ndx=str.index(r)
ndx?[ndx,ndx+str[r].大小-1]:无
结束
doit“Helloworld!”,“你好”
#=> [8,21]
发生的情况如下:

str  = "<strong>Hel</strong>lo World!"
word = "Hello"

a = word.chars
  #=> ["H", "e", "l", "l", "o"] 
s = a.join('(?:<.*?>)*')
  #=> "H(?:<.*?>)*e(?:<.*?>)*l(?:<.*?>)*l(?:<.*?>)*o" 
r = Regexp.new(s)
  #=> /H(?:<.*?>)*e(?:<.*?>)*l(?:<.*?>)*l(?:<.*?>)*o/ 
ndx = str.index(r)
  #=> 8 
t = str[r]
  #=> "Hel</strong>lo" 
o = t.size-1
  #=> 13 
ndx ? [ndx, ndx+str[r].size-1] : nil
  #=> 8 ? [8, 8 + t.size-1] : nil
  #=> [8, 8 + 14 -1] 
  #=> [8, 21] 
str=“Helloworld!”
word=“你好”
a=单词.chars
#=>[“H”、“e”、“l”、“l”、“o”]
s=a.join(“(?:)*”)
#=>“H(?)*e(?)*l(?)*l(?)*o”
r=Regexp.new(s)
#=>/H(?)*e(?)*l(?)*l(?)*o/
ndx=str.index(r)
#=> 8 
t=str[r]
#=>“Hello”
o=t.size-1
#=> 13 
ndx?[ndx,ndx+str[r].大小-1]:无
#=>8?[8,8+t.size-1]:无
#=> [8, 8 + 14 -1] 
#=> [8, 21] 

你能发布一个
定义的实际例子吗?
不必太长,但人们更容易根据真实的东西来测试答案,而不是猜测你在做什么。你试图解决的更高层次的问题是什么?也许你在这里尝试实施的解决方案不是唯一的/最好的一个。@MichałSzajbe试图提供类似于维基百科风格的文章间链接的功能,用户可以通过添加括号来使用标记来指示链接。但是,如果检测到匹配的名称,我们也可以自动添加标记。问题不清楚。是什么逻辑让您不将
作为链接匹配的一部分,但作为匹配的一部分?不清楚您的索引在尝试的哈希中指向什么。如果
“1”
转到
,但您有
“8”
转到
'H'
。这很聪明,我很想接受它,因为我认为我可以用一般的概念来做我想做的事情,但是有一些问题。这里没有考虑到这些问题,但应该能够相应地修改regexp。这将是我的问题;因为Cary的回答可以让你扫描不确定性ed string直接解决了扫描净化版本的问题吗?这当然是一个比我想象的更优雅的解决方案。也许是熟悉HTML的人(可能除了我以外的所有人都是这样)我可以发布一个使用这个想法但能正确实现的解决方案。我对你的正则表达式的能力感到敬畏。除了OP所说的实体之外,我唯一能看到的是它在任何情况下都需要找到字符串,而不管HTML标记如何。所以如果他们在搜索“世界”在上述情况下,它将失败。基本上按照您的方式进行,您需要一个正则表达式,使搜索对
中包含的任何内容都视而不见。然后,唯一的边缘情况将是实体和任何转义的
,它们是页面文本的一部分,而不是HTML。@Beartech我认为它不会找不到“世界”,键是零个或多个带括号的段。因此没有html标记是可以的。
definition = "Probati<strong>onary Peri</strong>od."
search_text = 'Probationary Period'
def doit(str, word)
  r = Regexp.new(word.chars.join('(?:<.*?>)*'))
  ndx = str.index(r)
  ndx ? [ndx, ndx+str[r].size-1] : nil
end

doit "<strong>Hel</strong>lo World!", "Hello" 
  #=> [8,21]
str  = "<strong>Hel</strong>lo World!"
word = "Hello"

a = word.chars
  #=> ["H", "e", "l", "l", "o"] 
s = a.join('(?:<.*?>)*')
  #=> "H(?:<.*?>)*e(?:<.*?>)*l(?:<.*?>)*l(?:<.*?>)*o" 
r = Regexp.new(s)
  #=> /H(?:<.*?>)*e(?:<.*?>)*l(?:<.*?>)*l(?:<.*?>)*o/ 
ndx = str.index(r)
  #=> 8 
t = str[r]
  #=> "Hel</strong>lo" 
o = t.size-1
  #=> 13 
ndx ? [ndx, ndx+str[r].size-1] : nil
  #=> 8 ? [8, 8 + t.size-1] : nil
  #=> [8, 8 + 14 -1] 
  #=> [8, 21]