Nasıl grup sembollere "değil aralığında" için

2 Cevap php

Nasıl PHP "aralıkta değil" ifadesi için grup sembolleri?

  1. liste öğesi 1
  2. liste öğesi 2
  3. liste öğesi 3
  4. liste öğesi 4
  5. ...

Ben "ikinci" = sınıfı ile biri hariç tüm li unsurları cath istiyorum

This does not work: /(<li[^(class="second")]+</li>)/xsU

2 Cevap

Muhtemelen böyle bir şey arıyor:

/<li(?:(?!class=['"]second['"])[^>])*>(?:(?!<\/li>).)*<\/li>/

Fikir yakalayan ifadeler içinde gruplandırılmış negatif İleriye iddialar kullanılarak dayanmaktadır. Burada karmaşık faktörler insanların HTML ayrıştırmak regexpi kullanarak karşı öneririz ana nedenlerinden biridir.

RegExp açıklanması:

/                - Start Regexp
 <li             - Match '<li'
 (?:             - Start Non-Capture Group
  (?!             - Start Negative Lookahead: 
                  - Ensure this group does not match (doesn't increment pointer either)
   class=          - Match 'class=' 
   ["']            - Match ' or "
   second          - Match 'second'
   ["']            - Match ' or "
  )               - End Negative Lookahead, 
                  - If it matched that whole segment, fail, otherwise:
  [^>]            - Match anything but '>'
 )*              - End Non-Capture Group - match 0 or more times
 >               - Match '>'
 (?:             - Start Non-Capture Group
  (?!             - Start negative lookahead
   <\/li>          - Match '</li>'
  )               - Ensure lookahead doesn't match, otherwise:
  .               - Capture any character
 )*              - End Non-Capture Group, match 0 or more times
 <\/li>          - Match '</li>'
/                - End RegExp

PHP "yerli" regex desteklemiyor olsa, preg_match sizin regex ifadesini yürütmek için kullanabilirsiniz.

Edit: ayrıca, erken çağrı sonlandırma, hangi sizin ifadede "/" in kaçan değiliz.