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.

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?

share|improve this question
    
Have you seen StringCases? The tutorials at the bottom of the StringCases documentation (specifically, the ones on String Patterns) will also come in useful. –  Aky Apr 4 at 17:46
    
@Aky I'm actually looking through that now, however it's not clear how to specify wildcard sequences flanking some character of interest? For example, this pattern "a" ~~ x_ ~~ "c" is workable, but how do we ask for something like my example ~~~x~~~~? –  CA30 Apr 4 at 17:47
    
@CA30 ~ joins patterns only. p.s. is each wildcard different? –  Kuba Apr 4 at 17:50
    
@Kuba Yes, here each 'wildcard' can be different. It just means "I don't care what's here, return it." But it does have to mean that 'some' character is there. We can't run off the ends of the string! –  CA30 Apr 4 at 17:51
2  
Try something like StringCases["acaqaddqccxjq", RegularExpression["...q..."], Overlaps -> True], which returns {"acaqadd", "addqccx"} –  Aky Apr 4 at 17:54

1 Answer 1

up vote 4 down vote accepted

Please reference: How do I perform string matching and replacements?

Using StringExpression:

string = ExampleData[{"Text", "DeclarationOfIndependence"}];

StringCases[string, _ ~~ _ ~~ _ ~~ "q" ~~ _ ~~ _ ~~ _]
{"d equal", " requir", "d equal", "linquis", "or quar", " acquie"}

Or:

StringCases[string, # ~~ "q" ~~ #] & @ Repeated[_, {3}]
{"d equal", " requir", "d equal", "linquis", "or quar", " acquie"}

Using RegularExpression as already shown by Aky:

StringCases[string, RegularExpression["...q..."]]
{"d equal", " requir", "d equal", "linquis", "or quar", " acquie"}

Or as I just (re)discovered using StringPattern`PatternConvert as noted in the comments below:

StringCases[string, RegularExpression[".{3}q.{3}"]]
{"d equal", " requir", "d equal", "linquis", "or quar", " acquie"}
share|improve this answer
    
Wasn't aware that Repeated worked with string patterns too.. +1. –  Aky Apr 4 at 20:11
1  
@Aky That's the beauty of StringExpression: you can use nearly all of the native Mathematica pattern operators in application to strings. Regular Expressions are often shorter for those well accustomed to them, but since I am not it usually takes me less time to write the StringExpression equivalent. Mathematica actually converts the StringExpression to a Regular Expression before handing off the operation to an optimized string library. You can see what is produced using StringPattern`PatternConvert. For example: (continued) –  Mr.Wizard Apr 4 at 20:18
3  
First @ StringPattern`PatternConvert[Repeated[_, {3}] ~~ "q" ~~ Repeated[_, {3}]] yields "(?ms).{3}q.{3}". You can use this to learn how to write RegularExpression patterns. (?ms) has to do with multi-line behavior; .{3} is the concise form of Repeated[_, {3}] in RE language. –  Mr.Wizard Apr 4 at 20:19
    
cool beans! I had no idea about all this. –  Aky Apr 4 at 20:22
    
@Aky As always, I'm glad I could help. :-) –  Mr.Wizard Apr 4 at 20:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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