2008-12-05 9 views
5

Je suis à la recherche d'un SimpleGrepSedPerlOrPythonOneLiner qui affiche toutes les citations dans un texte.Comment puis-je extraire toutes les citations dans un texte?


Exemple 1:

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

stdout:

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

Exemple 2:

cat MicrosoftWindowsXPEula.txt | SimpleGrepSedPerlOrPythonOneLiner 

stdout:

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

etc.

(link to the corresponding text).

Répondre

7

I like this:

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

Il est un peu bavard, mais il gère guillemets échappés et revenir en arrière beaucoup mieux que la mise en œuvre la plus simple. Qu'est-ce qu'il dit est:

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 vous n'avez pas besoin autant de pouvoir - dire qu'il est seulement susceptible d'être de dialogue et non citations structurées, puis

/"([^"]*)"/ 

fonctionne probablement aussi bien que tout autre.

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

Ce greps pour " + tout sauf une citation, un certain nombre de fois + "

Le -o fait que la sortie du texte correspondant, et non pas la ligne.

+0

Sous Windows '^' doit être échappé. 'chat eula.txt | grep -o "\" [^^ \ "] * \" "' – jfs

5

Aucune solution regexp ne fonctionnera que si vous avez des citations imbriquées, mais pour vos exemples, cela fonctionne 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

Sous Windows: 'cat eula.txt | perl -nE" dit $ 1 alors que/(\ "[^^ \"] * \ ")/g "' – jfs

+0

chat eula.txt | perl -lne 'imprimer pour /(".*?")/g' Perl golf FTW! ;) – 8jean

+0

Eh bien, certains moteurs regex gèrent des guillemets imbriqués, donc certaines solutions regex fonctionneront :) –

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

L'option '-o' imprimer seul modèle

Questions connexes