Extract a substring between two words from a string

r, regex, string, substr

Solution

Here is the regular expression way:

regmatches(string, regexpr('<body>.+<body>', string))

Problem

I have the following string: ``` string = "asflkjsdhlkjsdhglk<body>Iwant\to+extr@ctth!sstr|ng<body>sdgdfsghsghsgh" ``` I would like to extract the string between the two `<body>` tags. The result I am looking for is: ``` substring = "<body>Iwant\to+extr@ctth!sstr|ng<body>" ``` Note that the substring between the two `<body>` tags can contain letters, numbers, punctuation and special characters. Is there an easy way of doing this?

Original source