Say I'd like to pick substrings from some string such that these substrings have the pattern @@@q@@@
, where @
is used here as a stand-in for a 'wildcard' character (i.e. any character) and q
is just an example of a specific character that can be specified as desired.
The kind of lame solution might be as follows:
substringList = StringTake[testString, # + {-3, 3} & /@ StringPosition[testString, "q"]];
This, of course, runs into trouble when q
appears near one of the two ends of testString
.
What's the correct way to do this?
StringCases
? The tutorials at the bottom of theStringCases
documentation (specifically, the ones on String Patterns) will also come in useful. $\endgroup$~
joins patterns only. p.s. is each wildcard different? $\endgroup$StringCases["acaqaddqccxjq", RegularExpression["...q..."], Overlaps -> True]
, which returns{"acaqadd", "addqccx"}
$\endgroup$