[regex] Zoek string waarin een substring niet aanwezig is

Pagina: 1
Acties:

  • RwD
  • Registratie: Oktober 2000
  • Niet online

RwD

kloonikoon

Topicstarter
Ik ben op zoek naar een manier om mbv reguliere expressies een XML element te vinden waarbij een bepaald attribuut ontbreekt. Neem bijvoorbeeld een XML element <appel> nu wil ik graag dit element vinden als het attribuut 'type' ontbreekt. <appel type="fruit"> moet genegeerd worden maar <appel soort="granny"> moet gevonden worden.

Nu heb ik de referentie naar reguliere expressies er helemaal op doorzocht, maar ik kan alleen maar vinden hoe ik iets WEL match, niet hoe ik het omgekeerde bereik.

om even een kort voorbeeld te geven zal de expressie <appel[^t>]*type="fruit"[^>]*> een element vinden waar het type wel ingesteld is. Nu ben ik dus op zoek naar een expressie die appel vindt zonder het attribuut type

[ Voor 24% gewijzigd door RwD op 08-02-2005 13:01 ]


  • RwD
  • Registratie: Oktober 2000
  • Niet online

RwD

kloonikoon

Topicstarter
Ik heb na nog wat meer zoeken dit gevonden. Ik ga er nu soep van proberen te brouwen, maar als iemand toevallig de code al heeft is die ook welkom.

Een quote uit de bovenstaande link:
Positive and Negative Lookahead
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, I already explained why you cannot use a negated character class to match a "q" not followed by a "u". Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of round brackets, with the opening bracket followed by a question mark and an explanation point. Inside the lookahead, we have the trivial regex u.

Positive lookahead works just the same. q(?=u) matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of round brackets, with the opening bracket followed by a question mark and an equals sign.

You can use any regular expression inside the lookahead. (Note that this is not the case with lookbehind. I will explain why below.) Any valid regular expression can be used inside the lookahead. If it contains capturing parentheses, the backreferences will be saved. Note that the lookahead itself does not create a backreference. So it is not included in the count towards numbering the backreferences. If you want to store the match of the regex inside a backreference, you have to put capturing parentheses around the regex inside the lookahead, like this: (?=(regex)). The other way around will not work, because the lookahead will already have discarded the regex match by the time the backreference is to be saved.
Edit:
Ik heb het simpele voorbeeld uitgeprobeerd, en dat werkte niet. Helaas zal deze methode niet gaan werken en ook al heb ik het antwoord min of meer gevonden is dit voor mij geen oplossing :(

Toch iedereen bedankt voor de enorme hoeveelheid reacties ;) :+

[ Voor 9% gewijzigd door RwD op 08-02-2005 13:25 ]


  • muba
  • Registratie: April 2002
  • Laatst online: 19-10-2013

muba

Prince of Persia!

Regex is dan ook geen probaat middel om XML achtige dingen te parsen.
Welke taal werk je in? Heeft die taal geen modules/tools die dit vlekkeloos kunnen? Lijkt me een stuk makkelijker dan zelf een XML-parsing wiel uit te gaan vinden.

Reporter: Mister Gandhi, what do you think of western civilisation?
Gandhi: I think it would be a good idea