If I have some string data:
text = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 400]
"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume, among the Powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the sepa"
This is what I use to find a specific pattern inside and grab a bit more data around it:
StringCases[text, (y__ /;StringLength[y] == 4) ~~ "entitle" ~~ x__ /;StringLength[x] == 5]
{"God entitle them"}
Are there any alternatives, desirably neater ones? Maybe something with regular expressions?
Answer
Per Vitaliy's request:
With[{before = 4, after = 5},
StringTake[text, First[StringPosition[text, "entitle"]] + {-before, after}]]
yields "God entitle them"
.
Comments
Post a Comment