简体   繁体   English

Ruby gsub的正则表达式! 锚点不匹配

[英]Regular Expression for Ruby gsub! with anchor that should not match

The following regular expression should work in Ruby, but it doesn't. 以下正则表达式应在Ruby中起作用,但不起作用。 Any ideas on how to fix it, so it can be used in a .gsub! 关于如何修复它的任何想法,因此可以在.gsub!使用它.gsub! statement in a loop? 循环声明?

textfield.gsub!( /(http:\/\/){0}www\./, 'http://www.' )

{0} should allow to match the first part zero times, but it does not: {0}应该允许将第一部分匹配零次,但是不允许:

'http://www.company1.com
 http://www.company2.com'.gsub!( /(http:\/\/){0}www\./, 'http://www.' )

=> " http://http://www.company1.com http://http://www.company2.com =>“ http:// http://www.company1.com http:// http://www.company2.com

in this example the regexp should not match, and leave the input string unmodified! 在这个例子中,regexp不应该匹配,并且保持输入字符串不变!

any ideas on how to make this work? 关于如何进行这项工作的任何想法?

this looks like a bug in Ruby's regexp processing 这看起来像是Ruby的regexp处理中的错误

I admit that I'm trying to generously interpret the semantics of {n} to include n = 0 :) 我承认我试图慷慨地解释{n}的语义以包括n = 0 :)

The trouble is that /(http:\\/\\/){0}/ matches the start of any string. 问题是/(http:\\/\\/){0}/匹配任何字符串的开头。 In fact, /(x){0}/ will match the start of any string for any value of x . 实际上, /(x){0}/将匹配x任何值的任何字符串的开头。 This regular expression says that we should find x zero times. 这个正则表达式说我们应该找到x零次。 Well, we can find x zero times between any two characters. 好吧,我们可以找到两个字符之间的x零倍。

What you want is the start-of-string character, ^ , followed by a negative lookahead assertion, (?!...) . 您想要的是字符串开头字符^ ,后跟一个否定的超前断言(?!...) This allows you to match strings that do not begin with a particular sequence of characters. 这使您可以匹配不以特定字符序列开头的字符串。

'http://www.example.com'.gsub(/^(?!http:\/\/)www\./, 'http://www.')
# => 'http://www.example.com'

'www.example.com'.gsub(/^(?!http:\/\/)www\./, 'http://www.')
# => 'http://www.example.com'

Seems like you need to make the capturing group as optional. 似乎您需要将捕获组设为可选。

> 'http://www.example.com'.gsub(/(http:\/\/)?www\./, 'http://www.')
=> "http://www.example.com"
> 'www.example.com'.gsub(/(http:\/\/)?www\./, 'http://www.')
=> "http://www.example.com"

(http:\\/\\/)? matches the string http:// zero or one times. 匹配字符串http://零或一倍。

OR 要么

You could use negative lookbehind also. 您也可以使用负向后看。

> 'www.example.com'.gsub(/(?<!http:\/\/)www\./, 'http://www.')
=> "http://www.example.com"

Here the substitution should happen because the string www. 这里应该发生替换,因为字符串www. isn't preceded by http:// . 前面没有http://

> 'http://www.example.com'.gsub(/(?<!http:\/\/)www\./, 'http://www.')
=> "http://www.example.com"

Here the substitution won't happen because the string www. 这里不会发生替换,因为字符串www. is preceded by http:// . http://之前。 So the interpreter returns the original input string without any modifications. 因此,解释器将返回原始输入字符串,而无需进行任何修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM