0

入力:

<td>
<span>
<span>spanaaa</span>
<span class="1">spanbbb</span>
<span class="" style="">spanccc</span>
<span style="display:none">spanddd</span>

<div>divaaa</div>
<div class="1">divbbb</div>
<div class="" style="">divccc</div>
<div style="display:none">divddd</div>
</span>
</td>

属性 style="display:none" なしで値を取得するには、正規表現またはメソッドが必要です

出力:

spanaaa
spanbbb
spanccc

divaaa
divbbb
divcccc

4

4 に答える 4

1

パターン [.NET フレーバー]

(?<=<\w+ [^<>]*?\w+=")(?!display:none)(?<mt>[^"<>]+)(?=")

Options: ^ and $ match at line breaks

Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=<\w+ [^<>]*?\w+=")»
   Match the character “&lt;” literally «<»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the character “ ” literally « »
   Match a single character NOT present in the list “&lt;>” «[^<>]*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the characters “="” literally «="»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!display:none)»
   Match the characters “display:none” literally «display:none»
Match the regular expression below and capture its match into backreference with name “mt” «(?<mt>[^"<>]+)»
   Match a single character NOT present in the list “"<>” «[^"<>]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=")»
   Match the character “"” literally «"»

パターン[PCRE]

<!--
(<\w+ [^<>]*?\w+=")(?!display:none)([^"<>]+)(?=")

Options: ^ and $ match at line breaks

Match the regular expression below and capture its match into backreference number 1 «(<\w+ [^<>]*?\w+=")»
   Match the character “&lt;” literally «<»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the character “ ” literally « »
   Match a single character NOT present in the list “&lt;>” «[^<>]*?»
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the characters “="” literally «="»
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!display:none)»
   Match the characters “display:none” literally «display:none»
Match the regular expression below and capture its match into backreference number 2 «([^"<>]+)»
   Match a single character NOT present in the list “"<>” «[^"<>]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=")»
   Match the character “"” literally «"»
于 2012-04-18T05:44:30.633 に答える
0

正規表現はこれには適していません (HTML の気まぐれのため​​) が、これを試すことができます:

<div(?!\s*style="display:none")[^>]*>(.*?)</div>
于 2012-04-18T05:41:53.517 に答える
0
input = Regex.Replace(input, @"<div style=""display:none"">(.|\n)*?</div>", string.Empty, RegexOptions.Singleline);  

ここで入力は、Html を含む文字列です。この正規表現を試してみてください。

于 2012-12-06T07:07:17.373 に答える