Regex: trova quei particolari tag html che contengono queste parole (stringhe)

Aug 19 2020

Ho questo tag:

<span class="text_obisnuit2">* Not&#259;:</span>John Wells - <em>My Dreams</em>, Albatros Books, 1986.</p>

e questo:

<span class="text_obisnuit1">* Not&#259;:</span>Mariah Carey - <em>Lovers on the road</em>, BackStreet Books, 1965.</p>

Quindi, voglio trovare quei particolari tag html <span class="text_obisnuit2">che contengono queste parole (stringhe): Albatrose <em>e </em>(La prima riga)

Risposte

4 Glorfindel Aug 19 2020 at 16:49

Questo è semplice, ma richiede che "Albatros" venga dopo il <em>tag ( demo ):

(<span class="text_obisnuit2">).*<em>.*<\/em>.*Albatros.*

Il seguente non interessa in quale ordine sono ( demo ):

(<span class="text_obisnuit2">).*(<em>.*<\/em>.*Albatros.*|Albatros.*<em>.*<\/em>.*)

Ecco un'altra variazione, in cui le cifre dopo text_obisnuitnon contano e l'intero spantag viene catturato come primo gruppo ( demo ):

(<span class="text_obisnuit\d+">.*<\/span>).*(<em>.*<\/em>.*Albatros.*|Albatros.*<em>.*<\/em>.*)

Tutte le espressioni regolari presumono che le voci siano ciascuna sulla propria riga nel file. Forse ha più senso rilevare per <p>e </p>come i confini, ma per questo avremmo bisogno di un estratto più grande dal tuo file di input.

4 Toto Aug 19 2020 at 17:01
  • Ctrl+F
  • Trovare cosa: <span class="text_obisnuit2">(?=.*?<em>.*?</em>)(?=.*?\bAlbatros\b).*$
  • CONTROLLA Avvolgere
  • CONTROLLA Espressione regolare
  • UNCHECK . matches newline
  • Find All in Current Document

Spiegazione:

<span class="text_obisnuit2">   # literally
(?=                             # positive lookahead, make sure we have after:
.*?                           # 0 or more any character but newline, not greedy
<em>                          # literally open em tag
.*?                           # 0 or more any character but newline, not greedy
</em>                         # literally close em tag
)                               # end lookahead
(?=                             # positive lookahead, make sure we have after:
.*?                           # 0 or more any character but newline, not greedy
\bAlbatros\b                  # Albatros with word boundaries
)                               # end lookahead
.*                              # 0 or more any character but newline
$                               # end of line

Immagine dello schermo: