Quantcast
Channel: Active questions tagged string-manipulation - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 186

Extract substrings with defined length from a string

$
0
0

I have a string like this one

str = "this and that but also thit and that";

Now I want to extract, the first 3 letters before and after " and " so that the outcome is

{his and tha, hit and tha}

I tried it with

StringCases[str,  x__ ~~ " and " ~~ y__ :> {StringTake[x, -3], StringTake[y, 3]}]

but this extracts only the second substring {{"hit", "tha"}}. And using StringCases[str, _ ~~ " and " ~~ _] extracts only one letter {"s and t", "t and t"}. So is there a way to define a Blank with a particular length?


Viewing all articles
Browse latest Browse all 186

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>