Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have got a huge list with elements like this:

{":"ofa-legacy-conference",", ":"obama-2012",", ":"ofa-states","}

The first element is ":"ofa-legacy-conference"," the second ":"obama-2012"," and the last is ":"ofa-states",". In each element the first and the last double quotation is because the element is a string, but the inner ones are a part of the element.

My question is how may I implement StringCases to get the core of the elements without redundancies like the inner double quotations? The output would like this:

{"ofa-legacy-conference", "obama-2012", "ofa-states"}

share|improve this question

closed as unclear what you're asking by Kuba, Artes, Sjoerd C. de Vries, Jens, belisarius Aug 20 '13 at 21:43

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Are your inner quotes escaped (preceded by a backslash)? Otherwise, your list wouldn't be what you think it would be. –  bobthechemist Jul 25 '13 at 22:48
I do not understand what you mean!!! Just imagine I need the position of quotes!! How should I tell mathematica I need the quotes? like StringPosition[text," " "]? It doesn't work!!! –  Morry Jul 25 '13 at 22:50
The backslash didn't print in my comment. You need to tell Mathematica that the inner quotes are part of the string. –  bobthechemist Jul 25 '13 at 22:53
How should I?? It is the question somehow!! –  Morry Jul 25 '13 at 22:55
This might help, 2nd page. –  bobthechemist Jul 25 '13 at 23:17
show 3 more comments

2 Answers 2

up vote 3 down vote accepted

Assuming your list is a List of Strings -- which should be posted like this via copy/paste

{":\"ofa-legacy-conference\",", ":\"obama-2012\",", ":\"ofa-states\","}

-- then this works:

mylist = {":\"ofa-legacy-conference\",", ":\"obama-2012\",", ":\"ofa-states\","};
StringTrim[#, RegularExpression["[\":,]+"]] & /@ mylist
(* {"ofa-legacy-conference", "obama-2012", "ofa-states"} *)

This string pattern for StringTrim is somewhat more precise:

StringTrim[#, ":\"" | "\","] & /@ mylist

If your question is about how to use StringCases, then it could be done this way:

StringCases[#, RegularExpression["[^:,\"]+"]] & /@ mylist // Flatten

"Trimming" seems to fit the problem better than finding a list of "cases" for each string.

share|improve this answer
add comment

Maybe this could help you:

StringReplace[str,RegularExpression["^:\"(.+)\",$"]-> "$1"]

You get:


share|improve this answer
add comment

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