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 に答える