7
$\begingroup$

I actually deal with the problem of finding quotations (i.e. "this is a quotation") in larger texts of different languages, mostly German and English. Unfortunately there are several ways to fill text in quotation marks (marks differ in various languages). I tried StringCases, but this did not work as expected:

test = "This is a „real“ test, even wit a „really longer“ citation!"

then:

StringCases[test, "„" ~~ __ ~~ "“"]
(* {"„real“ test, even wit a „really longer\
“"} *)

which is correct, but not what I intended. I also tried with RegularExpression but could not get it to work as expected, so I got back to my "handmade" StringPosition which gave immediate success ;-)

pos = StringPosition[test, {"„", "“"}]

and

Partition[First /@ pos, 2]

finally:

StringTake[test, %]

This works, but I think this should be no problem with a RegularExpression or StringCases, only, ... I could not get it to work. Can anyone give me a hint (maybe it is obvious - but I do not see it)?

$\endgroup$

1 Answer 1

7
$\begingroup$

Perhaps Shortest is the key here:

str = "This is some text with \"quotations\" in «various styles», \
including \[OpenCurlyDoubleQuote]double curly quotes\
\[CloseCurlyDoubleQuote]."

StringCases[str, 
 Alternatives["\"", "'", "«", "\[OpenCurlyDoubleQuote]"] ~~ 
   Shortest[x___] ~~ 
   Alternatives["\"", "'", "»", "\[CloseCurlyDoubleQuote]"] :> x]

{"quotations", "various styles", "double curly quotes"}

Note this makes no attempt to pair the open and closing quotations so will not work with nested quotes.

$\endgroup$
1
  • $\begingroup$ Thank you so much! I missed the Shortest.. I always though that there must be simple solution, but I wouldn´t have found this. $\endgroup$
    – mgamer
    Commented Dec 13, 2015 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.