I'm trying to understand Mathematica's pattern matching in strings, but I have some doubts.
99% of the time I need to a) check if a match occurs and b) retrieve the matched patterns.
I come from perl, where it is easy to do both in two lines:
"big bad wolf =~ / (.*) /;
print"$1\n"
"bad"
In Mathematica is it possible to do it with just one function call? For now I'm able to do
a = "big bad wolf"
StringMatchQ[a, RegularExpression["big .* wolf"]]
True
StringCases[a, RegularExpression[" (.*) "] -> "$1"]
{"bad"}
As a 2nd question -- how can I retrieve more than one pattern, e.g. from this call:
StringCases[a, RegularExpression["(.*) .* (.*)"]
RegularExpression[]andStringCases[]already, why do they not suit your needs? If nothing is matched, you end up with an empty list. – J. M.'s missing motivation Jun 04 '15 at 08:33With[{s = StringCases[(* stuff *)]}, If[s =!= {}, s, False]]? – J. M.'s missing motivation Jun 04 '15 at 08:44RegularExpression. Frankly, for heavy duty reggae work, I prefer Ruby to Mathematica. – m_goldberg Jun 04 '15 at 13:35