Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi extract string between to 2 tags

Tags:

regex

delphi

How would I go about extracting text between 2 html tags using delphi? Here is an example string.

blah blah blah<tag>text I want to keep</tag>blah blah blah

and I want to extract this part of it.

<tag>text I want to keep</tag>

(basically removing all the blah blah blah garbage that comes before and after the <tag> & </tag> strings which I also want to keep.

Like I said, I am sure this is extremely easy for those who know, but I just cannot wrap my head around it at the moment. Thanks in advance for your replies.

like image 554
fubar Avatar asked Aug 30 '10 20:08

fubar


1 Answers

If you have Delphi XE, you can use the new RegularExpressions unit:

ResultString := TRegEx.Match(SubjectString, '(?si)<tag>.*?</tag>').Value;

If you have an older version of Delphi, you can use a 3rd party regex component such as TPerlRegEx:

Regex := TPerlRegEx.Create(nil);
Regex.RegEx := '(?si)<tag>.*?</tag>';
Regex.Subject := SubjectString;
if Regex.Match then ResultString := Regex.MatchedExpression;
like image 144
Jan Goyvaerts Avatar answered Sep 18 '22 12:09

Jan Goyvaerts