正则表达式 - 替换除 URL/URI 内的单词

发布于 2024-08-20 02:51:37 字数 126 浏览 8 评论 0原文

为 Web 应用程序编写全球化模块,我需要一个正则表达式来用另一个单词(翻译)替换一个单词的所有实例 - 除了在 URL/URI 中找到的单词。

编辑:我忘了提及我正在使用 Ruby,所以我不能使用“Lookbehind”

Writing a globalization module for a web application and I need a regexp to replace all instances of a word with another word (the translation) - except - words found within a URL/URI.

EDIT: I forgot to mention that I'm using Ruby, so I can't use 'Lookbehind'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伤痕我心 2024-08-27 02:51:37
  • 根据 URI 正则表达式进行拆分;在结果中包含 URI。
  • 对于每件作品:
    • 如果是 URI,则不用管它
    • 否则,进行单词替换
  • 加入片段

代码:

# From RFC 3986 Appendix B, with these modifications:
#   o Spaces disallowed
#   o All groups non-matching, except for added outermost group
#   o Not anchored
#   o Scheme required
#   o Authority required
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)"

def replace_except_uris(text, old, new)
  text.split(URI_REGEX).collect do |s|
    if s =~ URI_REGEX
      s
    else
      s.gsub(old, new)
    end
  end.join
end

text = <<END
stack http://www.stackoverflow.com stack
stack http://www.somewhere.come/stack?stack=stack#stack stack
END

puts replace_except_uris(text, /stack/, 'LINKED-LIST')

# => LINKED-LIST http://www.stackoverflow.com LINKED-LIST
# => LINKED-LIST http://www.somewhere.come/stack?stack=stack#stack LINKED-LIST
  • Split on URI regular expression; include the URI's in the result.
  • For each piece:
    • if it is a URI, leave it alone
    • otherwise, do word replacement
  • Join the pieces

Code:

# From RFC 3986 Appendix B, with these modifications:
#   o Spaces disallowed
#   o All groups non-matching, except for added outermost group
#   o Not anchored
#   o Scheme required
#   o Authority required
URI_REGEX = %r"((?:(?:[^ :/?#]+):)(?://(?:[^ /?#]*))(?:[^ ?#]*)(?:\?(?:[^ #]*))?(?:#(?:[^ ]*))?)"

def replace_except_uris(text, old, new)
  text.split(URI_REGEX).collect do |s|
    if s =~ URI_REGEX
      s
    else
      s.gsub(old, new)
    end
  end.join
end

text = <<END
stack http://www.stackoverflow.com stack
stack http://www.somewhere.come/stack?stack=stack#stack stack
END

puts replace_except_uris(text, /stack/, 'LINKED-LIST')

# => LINKED-LIST http://www.stackoverflow.com LINKED-LIST
# => LINKED-LIST http://www.somewhere.come/stack?stack=stack#stack LINKED-LIST
老子叫无熙 2024-08-27 02:51:37

您可能可以使用类似的东西

(?<!://[^ ]*)\bfoo\b

但这可能并不完美,它只是看起来该单词没有出现在之前没有 :// 的单个非空白字符串中这个词。

PS Home:\> "foo foobar http://foo_bar/baz?gak=foobar baz foo" -replace '(?<!://[^ ]*)\bfoo\b', 'FOO'
FOO foobar http://foo_bar/baz?gak=foobar baz FOO

You can probaby use something like

(?<!://[^ ]*)\bfoo\b

But this probably isn't perfect, it just looks that the word doesn't appear in a single non-whitespace string of characters that don't have :// somewhere before the word.

PS Home:\> "foo foobar http://foo_bar/baz?gak=foobar baz foo" -replace '(?<!://[^ ]*)\bfoo\b', 'FOO'
FOO foobar http://foo_bar/baz?gak=foobar baz FOO
坦然微笑 2024-08-27 02:51:37

您是否尝试过将文本拆分为单词并迭代单词?然后,您可以检查每个单词,确定它是否是 URI,如果不是,则进行翻译。

Have you tried splitting your text into words and iterating over the words? Then you can examine each word, determine if it's a URI, translate it if it isn't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文