Reguläre Ausdrücke / Parsen dig Ausgabe / Extrahieren von Text aus doppelten Anführungszeichen

StackOverflow https://stackoverflow.com/questions/1041199

  •  22-07-2019
  •  | 
  •  

Frage

Ich brauche Hilfe einige reguläre Ausdrücke herauszufinden. Ich bin mit dem Befehl dig und ich brauche seine Ausgabe zu verwenden. Ich brauche es, zu analysieren und erhalten es als ein Array mit PHP ordentlich angeordnet sind.

dig gibt so etwas wie folgt aus:

m0.ttw.mydomain.tel.    60      IN      TXT     ".tkw" "1" "20090624-183342" "Some text here1"
m0.ttw.mydomain.tel.    60      IN      TXT     ".tkw" "1" "20090624-183341" "Some text here2"

Ich möchte dies erhalten:

Array
(
    [0] => Array
        (
            [0] => .tkw
            [1] => 1
            [2] => 20090624-183342
            [3] => Some text here1
        )
    [1] => Array
...
)

Ich brauche nur den Inhalt innerhalb der doppelten Anführungszeichen. I können die dig Ausgabe Zeile für Zeile analysieren, aber ich denke, dass es schneller wäre, wenn ich nur die RegexMuster laufen alle es passend auf ...

Die Gedanken?

War es hilfreich?

Lösung

Die nahe kommt mit einer einzigen Zeile

preg_match_all( '/"([^"]+)"\s*"([^"]+)"\s*"([^"]+)"\s*"([^"]+)"/', $text, $matches, PREG_SET_ORDER );

print_r( $matches );

jedoch becuase, wie die preg_match * Funktionen arbeiten, wird die vollständige Übereinstimmung mit dem Index 0 jeder Match-Gruppe enthalten. Man könnte dieses Problem beheben, wenn Sie wirklich wollen.

array_walk( $matches, create_function( '&$array', 'array_shift( $array );return $array;' ) );

Andere Tipps

Ich bin nicht sicher über PHP reguläre Ausdrücke, aber in Perl die RE wäre einfach:

my $c = 0;
print <<EOF;
Array
(
EOF
foreach (<STDIN>) {
    if (/[^"]*"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"\s+"([^"]*)"/) {
        print <<EOF;
    [$c] => Array
        (
            [0] = $1
            [1] = $2
            [2] = $3
            [3] = $4
        )
EOF
        $c++;
    }
}

print <<EOF;
)
EOF

Dies hat einige Einschränkungen, nämlich:

  • Es funktioniert nicht, wenn der Text in Anführungszeichen entgangen Zitate haben kann (z \")
  • Es ist hart codiert nur vier angegebenen Werte zu unterstützen.

Code:

<?php
    $str = 'm0.ttw.mydomain.tel.    60      IN      TXT     ".tkw" "1" "20090624-183342" "Some text here1"
m0.ttw.mydomain.tel.    60      IN      TXT     ".tkw" "1" "20090624-183341" "Some text here2"';

    header('Content-Type: text/plain');
    $matches = array();
    preg_match_all('/(".*").*(".*").*(".*").*(".*")/U', $str, $matches, PREG_SET_ORDER);
    print_r($matches);
?>

Ausgabe:

Array
(
    [0] => Array
        (
            [0] => ".tkw" "1" "20090624-183342" "Some text here1"
            [1] => ".tkw"
            [2] => "1"
            [3] => "20090624-183342"
            [4] => "Some text here1"
        )

    [1] => Array
        (
            [0] => ".tkw" "1" "20090624-183341" "Some text here2"
            [1] => ".tkw"
            [2] => "1"
            [3] => "20090624-183341"
            [4] => "Some text here2"
        )

)

Insgesamt nicht das, was Sie gefragt, aber es funktioniert, könnte für Strings mit einer beliebigen Anzahl von Zitaten verwendet werden und hat den Vorteil, besser lesbar als der Durchschnittswert der regulären Ausdruck zu sein (auf Kosten von mehr Code Art und Weise)

class GetQuotedText {   
    const STATE_OUTSIDE = 'STATE_OUTSIDE';
    const STATE_INSIDE  = 'STATE_INSIDE';

    static private $input;
    static private $counter;
    static private $state;
    static private $results;

    static private $current;
    static private $full;
    static private $all;

    static private function setInput($string) {
        $this->input = $string;

    }

    static private function init($string) {
        self::$current  = array();
        self::$full         = array();      
        self::$input    = $string;
        self::$state    = self::STATE_OUTSIDE;
    }


    static public function getStrings($string) {
        self::init($string);
        for(self::$counter=0;self::$counter<strlen(self::$input);self::$counter++){
            self::parse(self::$input[self::$counter]);
        }
        self::saveLine();
        return self::$all;
    }

    static private function parse($char) {
        switch($char){
            case '"':
                self::encounteredToken($char);
                break;      
            case "\n":  //deliberate fall through for "\n" and "\r"
            case "\r":
                self::encounteredToken($char);
                break;
            default:
                if(self::$state == self::STATE_INSIDE) {
                    self::action($char);
                }
        }
    }

    static private function encounteredToken($token) {
        switch($token) {
            case '"':
                self::swapState();
                break;
            case "\n":  //deliberate fall through for "\n" and "\r"
            case "\r":
                self::saveArray();
                self::saveLine();
                break;
        }
        return;
    }

    static private function swapState() {
        if(self::$state == self::STATE_OUTSIDE) {
            self::$state = self::STATE_INSIDE;
        }
        else {
            self::$state = self::STATE_OUTSIDE;             
            self::saveArray();
        }               
    }
    static public function saveLine() {
        self::$all[] = self::$full;
        self::$full = array();
        //reset state when line ends
        self::$state = self::STATE_OUTSIDE;
    }

    static private function saveArray() {
        if(count(self::$current) > 0) {
            self::$full[]   = implode ('',self::$current);
            self::$current  = array();
        }
    }

    static private function action($char) {
        self::$current[] = $char;
    }
}

$input = 'm0.ttw.mydomain.tel.    60      IN      TXT     ".tkw" "1" "20090624-183342" "Some text here1"' . "\n" .
         'm0.ttw.mydomain.tel.    60      IN      TXT     ".tkw" "1" "20090624-183341" "Some text here2"';
$strings = GetQuotedText::getStrings($input);
print_r($strings);  
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top