2008-12-05 13 views
5

Estoy buscando SimpleGrepSedPerlOrPythonOneLiner que da salida a todas las citas en un texto.¿Cómo puedo extraer todas las citas en un texto?


Ejemplo 1:

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

stdout:

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

Ejemplo 2:

cat MicrosoftWindowsXPEula.txt | SimpleGrepSedPerlOrPythonOneLiner 

stdout:

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

etc.

(link to the corresponding text).

Respuesta

7

me gusta esto:

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

Es un poco prolijo, pero maneja cotizaciones escapó y retroceder mucho mejor que la implementación más simple. Lo que está diciendo es:

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; 

Si no es necesario que la cantidad de energía - decir que es probable que sólo sea cotizaciones de diálogo y no estructurados, a continuación,

/"([^"]*)"/ 

probablemente funcione tan bien como cualquier cosa más.

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

Este greps para " + nada más que una cita, cualquier número de veces + "

La -o hace que sea única salida de texto coincidente, no toda la línea.

+0

En Windows '^' debe ser escapado. 'cat eula.txt | grep -o "\" [^^ \ "] * \" "' – jfs

5

Sin solución de expresión regular funcionará si tiene citas anidadas, pero para sus ejemplos Esto funciona bien

$ 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

En Windows: 'cat eula.txt | perl -nE" dice $ 1 mientras/(\ "[^^ \"] * \ ")/g "' – jfs

+0

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

+0

Bueno, algunos motores regex manejan comillas anidadas, por lo que algunas soluciones de expresiones regulares funcionarán :) –

0
grep -o '"[^"]*"' file 

La opción '-o' de impresión único patrón

Cuestiones relacionadas