Domanda

Ho una regex che è andando a finire per essere un po ' più lunga e che sarebbe molto più facile da leggere per avere su più righe.

Ho provato questo, ma è solo barfs.

preg_match(
    '^J[0-9]{7}:\s+
    (.*?)             #Extract the Transaction Start Date msg
    \s+J[0-9]{7}:\s+Project\sname:\s+
    (.*?)             #Extract the Project Name
    \s+J[0-9]{7}:\s+Job\sname:\s+
    (.*?)             #Extract the Job Name
    \s+J[0-9]{7}:\s+',
    $this->getResultVar('FullMessage'),
    $atmp
);

Ci sono modo per passare una regex nel modulo di cui sopra preg_match?

È stato utile?

Soluzione

Si può usare la sintassi estesa:

preg_match("/
    test
/x", $foo, $bar);

Altri suggerimenti

Sì, è possibile aggiungere il /x Modello Modificatore.

Questo modificatore attiva funzionalità di PCRE che è incompatibile con Perl.Ogni barra rovesciata in un modello che è seguita da un lettera che non ha un significato particolare causa un errore, riservando questi combinazioni di espansione per il futuro.Da di default, come in Perl, una barra rovesciata seguito da una lettera, senza particolari significato viene considerato come un valore letterale.C' sono presenti altre caratteristiche controllata dal modificatore.

Per il tuo esempio, provate questo:

preg_match('/
              ^J[0-9]{7}:\s+
              (.*?)             #Extract the Transaction Start Date msg
              \s+J[0-9]{7}:\s+Project\sname:\s+
              (.*?)             #Extract the Project Name
              \s+J[0-9]{7}:\s+Job\sname:\s+
              (.*?)             #Extract the Job Name
              \s+J[0-9]{7}:\s+
            /x', $this->getResultVar('FullMessage'), $atmp);
  • Si dovrebbe aggiungere delimitatori:il primo carattere del regex saranno utilizzati per indicare la fine del modello.
  • Si dovrebbe aggiungere la 'x' di bandiera.Questo è lo stesso risultato mettendo (?x) all'inizio, ma è più leggibile imho.

OK, ecco la soluzione:

preg_match(
                '/(?x)^J[0-9]{7}:\s+
                (.*?)             #Extract the Transaction Start Date msg
                \s+J[0-9]{7}:\s+Project\sname:\s+
                (.*?)             #Extract the Project Name
                \s+J[0-9]{7}:\s+Job\sname:\s+
                (.*?)             #Extract the Job Name
                \s+J[0-9]{7}:\s+/'
                , $this->getResultVar('FullMessage'), $atmp);

La chiave è (?x) all'inizio che rende gli spazi insignificante e consente i commenti.

E ' anche importante che non c'è spazio tra l'inizio e fine di preventivi e la data di inizio e fine dell'espressione regolare.

Il mio primo tentativo in questo ha dato errori:

preg_match('
                /(?x)^J[0-9]{7}:\s+
                (.*?)             #Extract the Transaction Start Date msg
                \s+J[0-9]{7}:\s+Project\sname:\s+
                (.*?)             #Extract the Project Name
                \s+J[0-9]{7}:\s+Job\sname:\s+
                (.*?)             #Extract the Job Name
                \s+J[0-9]{7}:\s+/
           ', $this->getResultVar('FullMessage'), $atmp);

Cosa Konrad ha detto inoltre, funziona e si sente un po ' più facile che attaccare (?x) all'inizio.

In PHP il commento sintassi simile a questa:

(?# Your comment here)

preg_match('
            ^J[0-9]{7}:\s+
            (.*?)             (?#Extract the Transaction Start Date msg)
            \s+J[0-9]{7}:\s+Project\sname:\s+
            (.*?)             (?#Extract the Project Name)
                \s+J[0-9]{7}:\s+Job\sname:\s+
            (.*?)             (?#Extract the Job Name)
            \s+J[0-9]{7}:\s+
            ', $this->getResultVar('FullMessage'), $atmp);

Per ulteriori informazioni, vedere la PHP Sintassi delle Espressioni Regolari di Riferimento

È inoltre possibile utilizzare il PCRE_EXTENDED (o 'x') Modello Modificatore come indica il suo esempio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top