0

我想从我的字符串中匹配像<word>~, <word>~0.1,这样<word>~0.9的字符串。

"<word>~0.5"但如果它在双引号内,如or ,则不应匹配"<word>"~0.5

几个例子:

"World Terror"~10 AND music~0.5                --> should match music~0.5
"test~ my string"                              --> should not match
music~ AND "song remix" AND "world terror~0.5" --> should match music~

我现在已经应用了下面的正则表达式\w+~,但如果匹配包含在引号内,它也匹配。

请问有人可以帮我吗?

4

1 回答 1

2

这适用于不包含转义引号的字符串(因为这些字符串会使偶数引号的计数失去平衡):

Regex regexObj = new Regex(
    @"\w+~[\d.]*  # Match an alnum word, tilde, optional digits/dots
    (?=           # only if there follows...
     [^""]*       # any number of non-quotes
     (?:          # followed by...
      ""[^""]*    # one quote, and any number of non-quotes
      ""[^""]*    # another quote, and any number of non-quotes
     )*           # any number of times, ensuring an even number of quotes
     [^""]*       # Then any number of non-quotes
     $            # until the end of the string.
    )             # End of lookahead assertion", 
    RegexOptions.IgnorePatternWhitespace);

如果需要解决转义的引号,它会变得有点复杂:

Regex regexObj = new Regex(
    @"\w+~[\d.]*         # Match an alnum word, tilde, optional digits/dots
    (?=                  # only if there follows...
     (?:\\.|[^\\""])*    # any number of non-quotes (or escaped quotes)
     (?:                 # followed by...
      ""(?:\\.|[^\\""])* # one quote, and any number of non-quotes
      ""(?:\\.|[^\\""])* # another quote, and any number of non-quotes
     )*                  # any number of times, ensuring an even number of quotes
     (?:\\.|[^\\""])*    # Then any number of non-quotes
     $                   # until the end of the string.
    )                    # End of lookahead assertion", 
    RegexOptions.IgnorePatternWhitespace);
于 2012-10-30T09:35:58.500 回答