Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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_Regex - Fatal编程技术网

Ruby 正则表达式获取引号外的子字符串

Ruby 正则表达式获取引号外的子字符串,ruby,regex,Ruby,Regex,是否有正则表达式将字符串中所有提及的子字符串替换为除引号“中的子字符串之外的其他子字符串 例如,如果子字符串为“Hello”,替换的字符串为“World”: Hello Everybody“Hello”Everybody应该返回World Everybody“Hello”Everybody 很抱歉刚才的措辞,我发了个屁 ^([^"]*"[^"]*")*[^"]*hello 只有在前面有偶数个引号字符(包括0)时,此正则表达式才会与“hello”匹配(这表示它当前不在引号内) 要分解它: ^:

是否有正则表达式将字符串中所有提及的子字符串替换为除引号
中的子字符串之外的其他子字符串

例如,如果子字符串为“Hello”,替换的字符串为“World”:
Hello Everybody“Hello”Everybody
应该返回
World Everybody“Hello”Everybody

很抱歉刚才的措辞,我发了个屁

^([^"]*"[^"]*")*[^"]*hello
只有在前面有偶数个引号字符(包括0)时,此正则表达式才会与“hello”匹配(这表示它当前不在引号内)

要分解它:

  • ^:从字符串的开头开始
  • (:启动可以重复任意次数的quotes组
  • [^“]*:为引号之前的任意文本匹配任意数量的非引号字符
  • “:匹配起始报价
  • [^”]*:匹配任意数量的非引号字符(这是引号中的内容)
  • “:匹配结束报价
  • )*:结束quotes组并重复任意次数,包括0
  • [^”]*:为单词前的任意文本匹配任何其他非引号字符
  • 您好:您好
与此处的其他答案不同,这将在以下情况下成功:

"apple"hello"world"
因为
apple
world
在引号中,而不是hello

而这将失败于

"apple hello world"

因为hello仍然在引号中

您可以对
使用否定的后向和前向断言,如下所示:

(?<!["'])(Hello)(?!["'])
编辑:

因此,在一种情况下:

示例字符串:
Hello Everybody“Hello”Everybody

替换为:
(无)

输出:

Everybody "Hello" Everybody

使用消极的前向和消极的后向断言。这是有效的

irb(main):050:0> testcases = ["hello world", "'hello world", "'hello' world", "hello' world", "hello\"world"]
=> ["hello world", "'hello world", "'hello' world", "hello' world", "hello\"world"]
irb(main):051:0> testcases.each { |i| puts /(?<!')(?<!")hello(?!")(?!')/.match(i) }
hello




=> ["hello world", "'hello world", "'hello' world", "hello' world", "hello\"world"]
irb(main):050:0>testcases=[“你好世界”,“你好世界”,“你好世界”,“你好世界”,“你好世界”,“你好世界”]
=>[“你好世界”,“你好世界”,“你好世界”,“你好世界”,“你好世界”,“你好\”世界]
irb(main):051:0>testcases.each{i | put/(?[“你好世界”,“你好世界”,“你好世界”,“你好世界”,“你好世界”,“你好世界”]

编辑:更新为包含双引号,但可以扩展为任何字符。

说whaaat:
“你好”世界"
删除引号内的子字符串…不是我想要的
那么你想要的是什么?我只想忽略子字符串,但是如果引号为奇数,你会忽略哪个子字符串?更好的是,如果有多组引号,你会忽略哪个子字符串?我不理解这个例子。你不应该忽略吗预期字符串包括“world”,因为在输入字符串中有一个不带引号的实例?此外,目标字符串是“hello”(小写),但输入字符串中的单词是“hello”(大写)。是否应忽略大小写?即使(可以说)与
“apple”hello“world”
不匹配引号中是苹果和世界。@DamienBlack,没错,OP不希望引号中的任何内容匹配。因此,
“苹果”Hello“世界”
将不匹配。但是
Hello
不在引号中。引号中是“苹果”和“世界”。这也不会匹配
Hello“世界”
,这里显然是“世界”在引号中,而不是hello。它在
测试hello world时也会成功
,它不应该“t这不起作用,它在
hello'world'上失败了
它在
test hello world'上也成功了,它不应该“t”这一概念起作用,我甚至解释了负前瞻的概念,这是一个高级regexp概念。它可以被更新以包含额外的字符。为什么否决?应该没有她在我上面的评论中引用了“测试hello world”
你似乎在胡扯,因为你的答案没有被接受,我实际上花了时间解释了一下。
irb(main):050:0> testcases = ["hello world", "'hello world", "'hello' world", "hello' world", "hello\"world"]
=> ["hello world", "'hello world", "'hello' world", "hello' world", "hello\"world"]
irb(main):051:0> testcases.each { |i| puts /(?<!')(?<!")hello(?!")(?!')/.match(i) }
hello




=> ["hello world", "'hello world", "'hello' world", "hello' world", "hello\"world"]