5

Hi i'm new to programming and it's my first question, so please go easy on me. I want to create a function that extracts text which is between BEGIN and >. For example with a string of text:

str="Random stuff here TEXT BEGIN extract this text > here is other stuff >"

The function should extract the text between BEGIN and >. The text in-between, can be anything from symbols like \n or be another string.

I know I can get every instance of BEGIN with position,

Position[StringSplit[str],"BEGIN"]

but how do I get the position of the next instance of > from BEGIN, so I can extract the text in-between? Or is there a smarter way of doing this?

kglr
  • 394,356
  • 18
  • 477
  • 896
joweyedete
  • 53
  • 3

2 Answers2

7
StringCases[str, "BEGIN" ~~ x : Shortest[___] ~~ ">" :> x]

{" extract this text "}

StringCases["Random stuff here TEXT BEGIN extract \n this text > here is other stuff >", 
 "BEGIN" ~~ x : Shortest[___] ~~ ">" :> x]

{" extract
this text "}

To trim the white space around extracted string use StringTrim on the previous results or on the right hand side of the replacement rule:

StringCases[str, "BEGIN" ~~ x : Shortest[___] ~~ ">" :> StringTrim[x]]

{"extract this text"}

kglr
  • 394,356
  • 18
  • 477
  • 896
1
str = "Random stuff here TEXT BEGIN extract this text > here is other stuff >";

StringTake[str, #]&[Last@First@StringPosition[str, #] & /@ {"BEGIN", ">"} + {2, -2}]
eldo
  • 67,911
  • 5
  • 60
  • 168