Topic: TRegExp and StrReplace

Hello Dmitry,


TRegExp was a real breakthrough for me because it helped me scraping web pages more easily.


But sometimes, instead of matching strings between matched searches, we want to replace a pattern with something else into a large string.
As an example, stripping HTML tags from a large text.


One RegEx pattern for matching any html tag without having to worry for what's inside the < > is :

<[^<>]+>

With this pattern, I'd like to simply delete the HTML tags but

ReplaceStr(detail_string,reg3.match,'');

does not work because reg3.match is a TStringList and detail_string is a string.


Could we have something like ?

for i := 0 to reg3.match.Count -1 do
     Reg3.Replace(detail_string, '<[^<>]+>', '');

(or whatever the loop you use)


Thanks for reading and have a good day smile


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

2 (edited by mathmathou 2017-01-17 04:39:35)

Re: TRegExp and StrReplace

Sorry, I searched a bit and found a way without TRegEx.Replace :

reg3 := TRegExp.Create('<[^<>]+>');
     reg3.InputString := detail_string;
          if reg3.Exec then
               repeat
                      detail_string := ReplaceStr(detail_string,reg3.Match[0],'');
               until not reg3.ExecNext;

Auto corrected question =)

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor