2010-08-29 13 views
6

Questa è un'espressione regolare ben documentata, facile da capire, mantenere e modificare.Come si capiscono le espressioni regolari scritte in una riga?

text = text.replace(/ 
    (        // Wrap whole match in $1 
     (
      ^[ \t]*>[ \t]?   // '>' at the start of a line 
      .+\n     // rest of the first line 
      (.+\n)*     // subsequent consecutive lines 
      \n*      // blanks 
     )+ 
    ) 
    /gm, 

Ma come si lavora con questi?

text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm, 

c'è una beautifier di qualche tipo che ha un senso di esso e descrive la sua funzionalità?

+1

Forse alcuni strumenti in http://stackoverflow.com/questions/32282/regex-testing-tools aiuterà. – kennytm

+0

Non ne faccio nessuno. Io uso sempre 6-10 linee di codice con explode/join/strstr/substr (PHP). Più facile da capire, mantenere e persino scrivere. – ern0

+0

Non tutte le lingue o le librerie che supportano l'espressione regolare funzioneranno in modo così semplice come il tuo esempio, a causa degli spazi bianchi aggiunti. –

risposta

3

RegexBuddy "tradurrà" qualsiasi regex per te. Quando alimentate il vostro esempio regex, emette:

((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+) 

Options:^and $ match at line breaks 

Match the regular expression below and capture its match into backreference number 1 «((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)» 
    Match the regular expression below and capture its match into backreference number 2 «(^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Note: You repeated the capturing group itself. The group will capture only the last iteration. 
      Put a capturing group around the repeated group to capture all iterations. «+» 
     Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
     Match a single character present in the list below «[ \t]*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
     The character “ ” « » 
     A tab character «\t» 
     Match the character “>” literally «>» 
     Match a single character present in the list below «[ \t]?» 
     Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
     The character “ ” « » 
     A tab character «\t» 
     Match any single character that is not a line break character «.+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Match a line feed character «\n» 
     Match the regular expression below and capture its match into backreference number 3 «(.+\n)*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
     Note: You repeated the capturing group itself. The group will capture only the last iteration. 
      Put a capturing group around the repeated group to capture all iterations. «*» 
     Match any single character that is not a line break character «.+» 
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Match a line feed character «\n» 
     Match a line feed character «\n*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 

Questo ha un aspetto piuttosto intimidatorio in forma di testo, ma è molto più leggibile in forma HTML (che non può essere riprodotto qui) o in RegexBuddy sé. Indica anche trucchi comuni (come la ripetizione di gruppi di cattura che probabilmente non sono desiderati qui).

+0

Wow, è piuttosto dettagliato. Ma potrebbe essere utile per imparare regex. –

+0

Francamente parlando, trovo che questa spiegazione pesante non sia molto più comprensibile della regex a 1 riga. – kennytm

0

Dopo un po ', mi sono abituato a leggere le cose. Non c'è molto da fare nella maggior parte delle regex e consiglio il sito http://www.regular-expressions.info/ se si desidera utilizzarle più spesso.

5

Vale la pena lo sforzo di diventare esperti nella lettura delle espressioni regolari nel modulo a una riga. Il più delle volte ci sono scritti in questo modo

+0

Sì proprio come qualsiasi sintassi del linguaggio di programmazione, dopo un po 'diventa leggibile. – Chris

0

Le espressioni regolari sono solo un modo per esprimere maschere, ecc. Alla fine è solo un "linguaggio" con una propria sintassi.
Commento ogni bit della tua espressione regolare sarebbe la stessa cosa di commentare ogni riga del tuo progetto.
Naturalmente aiuterebbe le persone che non capiscono il tuo codice, ma è solo inutile se tu (lo sviluppatore) capisci il significato della regex.

Per me, leggere le espressioni regolari è la stessa cosa del codice di lettura. Se l'espressione è davvero complessa, una spiegazione sotto potrebbe essere utile ma la maggior parte delle volte non è necessaria.

Problemi correlati