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? Thank you!
Here is the regular expression way:
regmatches(string, regexpr('<body>.+<body>', string))
regex = '<body>.+?<body>'
You want the non-greedy (.+?
), so that it doesn't group as many <body>
tags as possible.
If you're solely using a regex with no auxiliary functions, you're going to need a capturing group to extract what is required, ie:
regex = '(<body>.+?<body>)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With