2008-12-05 13 views
5

Szukam SimpleGrepSedPerlOrPythonOneLiner, który wypisze wszystkie cytaty w tekście.Jak mogę wyodrębnić wszystkie cytaty z tekstu?


Przykład 1:

echo “HAL,” noted Frank, “said that everything was going extremely well.” | SimpleGrepSedPerlOrPythonOneLiner 

standardowe wyjście:

"HAL," 
"said that everything was going extremely well.” 

Przykład 2:

cat MicrosoftWindowsXPEula.txt | SimpleGrepSedPerlOrPythonOneLiner 

standardowe wyjście:

"EULA" 
"Software" 
"Workstation Computer" 
"Device" 
"DRM" 

itp

(link to the corresponding text).

Odpowiedz

7

Lubię to:

perl -ne 'print "$_\n" foreach /"((?>[^"\\]|\\+[^"]|\\(?:\\\\)*")*)"/g;' 

To trochę rozwlekły, ale obsługuje on uciekł cytaty i cofanie się o wiele lepiej niż najprostsza implementacja. Co ona mówi jest:

my $re = qr{ 
    "    # Begin it with literal quote 
    ( 
    (?>   # prevent backtracking once the alternation has been 
        # satisfied. It either agrees or it does not. This expression 
        # only needs one direction, or we fail out of the branch 

     [^"\\] # a character that is not a dquote or a backslash 
    | \\+  # OR if a backslash, then any number of backslashes followed by 
     [^"]  # something that is not a quote 
    | \\  # OR again a backslash 
     (?>\\\\)* # followed by any number of *pairs* of backslashes (as units) 
     "   # and a quote 
    )*   # any number of *set* qualifying phrases 
)    # all batched up together 
    "    # Ended by a literal quote 
}x; 

Jeśli nie trzeba, że ​​dużo energii - mówią, że to prawdopodobnie tylko być dialog, a nie cytaty strukturyzowane, następnie

/"([^"]*)"/ 

prawdopodobnie działa prawie tak dobrze, jak wszystko jeszcze.

4
grep -o "\"[^\"]*\"" 

ten greps dla " + niczego oprócz cytat, dowolną ilość razy + "

-o sprawia, że ​​wyjście tylko dopasowany tekst, a nie cała linia.

+0

W Windows '^' musi być uciekł. 'cat eula.txt | grep -o "\" [^^ \ "] * \" "' – jfs

5

Brak rozwiązania regexp będzie działać, jeśli masz zagnieżdżone cytaty, ale dla przykładów to działa dobrze

$ echo \"HAL,\" noted Frank, \"said that everything was going extremely well\" 
| perl -n -e 'while (m/(".*?")/g) { print $1."\n"; }' 
"HAL," 
"said that everything was going extremely well" 

$ cat eula.txt| perl -n -e 'while (m/(".*?")/g) { print $1."\n"; }' 
"EULA" 
"online" 
"Software" 
"Workstation Computer" 
"Device" 
"multiplexing" 
"DRM" 
"Secure Content" 
"DRM Software" 
"Secure Content Owners" 
"DRM Upgrades" 
"WMFSDK" 
"Not For Resale" 
"NFR," 
"Academic Edition" 
"AE," 
"Qualified Educational User." 
"Exclusion of Incidental, Consequential and Certain Other Damages" 
"Restricted Rights" 
"Exclusion des dommages accessoires, indirects et de certains autres dommages" 
"Consumer rights" 
+0

W systemie Windows: 'cat eula.txt | perl -nE" powiedz $ 1 podczas/(\ "[^^ \"] * \ ")/g "' – jfs

+0

cat eula.txt | perl -lne 'print dla /(".*?")/g' Perl golf FTW! ;) – 8jean

+0

Cóż, niektóre silniki regex obsługują zagnieżdżone cudzysłowy, więc niektóre rozwiązania będą działać poprawnie :) –

Powiązane problemy