用Regex替换动态字符串

时间:2013-08-18 18:41:04

标签: regex vb.net replace

我正在使用Visual Basic .NET而我正在尝试下载一串HTML,我想替换它

    id="dynamicstring"

使用

    id="replacement"

动态字符串可以是任何东西,这就是我无法替换它的原因。

1 个答案:

答案 0 :(得分:0)

您可以将id属性的内容与此模式匹配:

(?<=<div\b(?>[^i]+|\Bi|i(?!d\s*=))*id\s*=\s*")[^"]+

细节:

(?<=                 # open a look behind assertion (it's just a check
                     # nothing is matched inside it)
    <div\b           # div tag
    (?>              # atomic group (all the content until the id attribute
        [^i]+        # all that is not a "i"
      |              # OR
        \Bi          # a "i" not preceded by a word boundary
      |              # OR
        i(?!d\s*=)   # a "i" (with an implicite word boundary) 
                     # not followed by "d="
    )*               # close the atomic group and repeat as necessary
    id\s*=\s*"       # the id attribute until the first double quote
)                    # close the lookbehind
[^"]+                # content of the id attribute
                     # (all that is not a double quote)