【问题标题】:Ruby Regexp#match to match start of string with given position (Python re-like)Ruby Regexp#match 匹配字符串的开头与给定的位置(Python re-like)
【发布时间】:2012-11-19 11:19:28
【问题描述】:

我正在寻找一种从第一个符号匹配字符串的方法,但考虑到我给 match 方法的偏移量。

test_string = 'abc def qwe'
def_pos = 4
qwe_pos = 8

/qwe/.match(test_string, def_pos) # => #<MatchData "qwe">
# ^^^ this is bad, as it just skipped over the 'def'

/^qwe/.match(test_string, def_pos) # => nil
# ^^^ looks ok...

/^qwe/.match(test_string, qwe_pos) # => nil
# ^^^ it's bad, as it never matches 'qwe' now

我正在寻找的是:

/...qwe/.match(test_string, def_pos) # => nil
/...qwe/.match(test_string, qwe_pos) # => #<MatchData "qwe">

有什么想法吗?

【问题讨论】:

    标签: ruby regex


    【解决方案1】:

    使用字符串切片怎么样?

    /^qwe/.match(test_string[def_pos..-1])
    

    pos 参数告诉正则表达式引擎从哪里开始匹配,但它不会改变行首(和其他)锚点的行为。 ^ 仍然只匹配一行的开头(并且qwe_pos 仍然在test_string 的中间)。

    另外,在 Ruby 中,\A 是“字符串开头”锚点,\z 是“字符串结尾”锚点。 ^$ 也匹配行的开始/结束,并且没有选项可以改变这种行为(这对 Ruby 来说是特殊的,就像 (?m) 的迷人令人困惑的用法一样,它完成了 (?s) 在其他正则表达式风格)...

    【讨论】:

    • 好的,谢谢。在类似的问题中得到相同的想法:stackoverflow.com/questions/7292976/…。现在只需要计算它对 BIG 字符串的性能影响
    • 好吧,似乎对性能影响不大,我在分析中又赢了 1 秒 :)
    • 感谢 \A 补充。已经知道为什么 ^(\n+) 组在“
    • @Farcaller perf 对只读字符串切片的影响应该很小,新的字符串对象将引用原始字符串的字符串存储区域,并带有适当的新偏移量和长度元数据。它设置了一个 shared 标志,因此如果原始字符串或切片字符串发生后续突变,则可以遵循适当的写时复制语义。
    • 感谢您的评论。不确定红宝石字符串是 CoW。
    猜你喜欢
    • 2010-12-07
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多