How to extract all sub-strings of string with a specified start and end using StringCases? [duplicate]

How can I find all the (non-overlapping) substrings that start with a specific string of characters (e.g. ">>") and end with another specified string (e.g. "<<"?

For example, when I try the following,

StringCases["ccc>>ccccccc<<cccccc>>cccccccccccccc<<ccccc",">>" ~~ __ ~~ "<<"]


I get,

{">>ccccccc<<cccccc>>cccccccccccccc<<"}


but I want

{">>ccccccc<<", ">>cccccccccccccc<<"}.


Furthermore, how do I add a pattern that chooses strings smaller than 10 characters?

{">>ccccccc<<"}

• See Shortest. StringCases["ccc>>ccccccc<<cccccc>>ccccccc<<ccccc", ">>" ~~ Shortest[__] ~~ "<<"] – Jason B. Aug 8 '17 at 1:57
• That works. Thanks. – Miladiouss Aug 8 '17 at 1:59
• Regex: StringCases["ccc>>ccccccc<<cccccc>>cccccccccccccc<<ccccc", RegularExpression[">>(.{1,9})<<"]] – J. M.'s technical difficulties Aug 8 '17 at 2:49
• How can I learn more about string patterns. I usually enjoy reading the Mathemacia documentations, but their string pattern tutorial is not very engaging and informative. Any recomendations? – Miladiouss Aug 9 '17 at 8:56
• @mpourrah String patterns are converted into regular expressions and then passed to the PCRE library. Hence in order to understand how they work it is worth to learn regexes. There are great resources on the web for this, for example rexegg.com. For testing how string patterns are converted into regexes there is StringPatternPatternConvert. – Alexey Popkov Aug 10 '17 at 1:53

If you are guaranteed that all the opening ">>" and closing "<<" tags are exactly balanced without nesting, then the solutions suggested in the comments will give you the shortest substring between the tags:

StringCases["ccc>>ccccccc<<cccccc>>ccccccc<<ccccc", ">>" ~~ Shortest[___] ~~ "<<"]

{">>ccccccc<<", ">>ccccccc<<"}


(I assume that you wish to obtain all the substrings between the tags including zero-length substrings. If you don't want the latter, replace ___ with __ in the string patterns and * with + in the regexes.)

But in a case of non-balanced or nested tags they will fail to give you the shortest substring between the tags:

StringCases["c>>cc>>ccccccc<<cccccc>>ccccccc<<ccccc", ">>" ~~ Shortest[___] ~~ "<<"]

{">>cc>>ccccccc<<", ">>ccccccc<<"}


In such a situation you can use one of the solutions suggested here:

StringCases["c>>cc>>ccccccc<<cccccc>>ccccccc<<ccccc",
">>" ~~ RegularExpression["(?:(?!>>).)*?"] ~~ "<<"]

{">>ccccccc<<", ">>ccccccc<<"}


If you need only substrings with length smaller than 10 characters you can use either the two-argument form of Repeated or the corresponding regex statement:

(* well-balanced tags without nesting *)
StringCases["ccc>>ccccccc<<cccccc>>ccccccc<<ccccc",
">>" ~~ Shortest[Repeated[_, {0, 9}]] ~~ "<<"]

{">>ccccccc<<", ">>ccccccc<<"}

(* non-balanced or nested tags *)
StringCases["c>>cc>>ccccccc<<cccccc>>ccccccc<<ccccc",
">>" ~~ RegularExpression["(?:(?!>>).){0,9}?"] ~~ "<<"]

{">>ccccccc<<", ">>ccccccc<<"}

• When we process this string "xx >>aa >>bbb<< >>bbb<< aa<< yy>>>>dfa<<a<<", actually the following result is expected.But the MMA seem don't support the balance group syntax. – yode Aug 8 '17 at 6:18
• @yode You are asking for the longest substring between balanced tags, this case is addressed here. As I understand, the OP wants the shortest substring. – Alexey Popkov Aug 8 '17 at 6:37
• Ok,I fixed it in the new edit. – yode Aug 8 '17 at 7:32

For the non-balanced or nested tags,we can use ?P

StringCases["xx >>aa >>bbb<< >>bbb<< aa<< yy>>>>dfa<<a<<",
RegularExpression["(?P<a>>>([^<>]|(?P>a))*<<)"]]


{">>aa >>bbb<< >>bbb<< aa<<", ">>>>dfa<<a<<"}

Or ?R

StringCases["xx >>aa >>bbb<< >>bbb<< aa<< yy>>>>dfa<<a<<",
RegularExpression[">>[^<>]*((?R)|[^<>])*<<"]]


will give a same result.

For the shortest substring we have more easy solution

StringCases["xx >>aa >>bbb<< >>bbb<< aa<< yy>>>>dfa<<a<<",
RegularExpression[">>.+?<<"]]


{">>aa >>bbb<<", ">>bbb<<", ">>>>dfa<<"}

• It is a good habit to provide a link to the original answer from where the solution comes. Also it is worth to point out that this solution will give the longest substring between balanced tags (i.e. with nested balanced tags) as opposed to my answer which gives the shortest substring between the tags. – Alexey Popkov Aug 8 '17 at 6:46
• Sorry,I just forget the original post,that hard for me to find those link,I alway confusing you and Cuba how to find it so quick.Actually it is from my personal notes.. – yode Aug 8 '17 at 6:57
• "how to find it so quick": in my personal notes I always include a link to the post from where I learned the technique. Without such a link it is difficult to understand the idea if I forgotten how the code works. – Alexey Popkov Aug 8 '17 at 7:03
• @AlexeyPopkov If I have a misunderstand the OP.Do you think I should delete this answer?Actually that post don't include the method based on ?R`.. – yode Aug 8 '17 at 7:06
• Since the question is formulated in a confusing way (with insufficient information), your answer is appropriate. You shouldn't delete it. – Alexey Popkov Aug 8 '17 at 7:09