Forum > General
TRegExpr Help
Tony Stone:
So I am using the TRegExpr unit to try and match parts of strings and surround the matched strings in some HTML. Seems straight forward and maybe it is my expressions that are an issue so here is 2 of the various expressions I am using
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---.*tony.* ilazarus.*
Now I struggle with regular expressions but the .*tony.* I understand should match "xxxxxxxxxToNyxxxxxxx" or "tonyxxxxx" ?
And lazarus.* would match "lazarusxxxxxxxxxxx" but not "LaZarusxxxxxxxxxx" ?
I'm assuming those are ok enough for my current test.
The code to wrap the matched text has varried but currently I am at this point and I am getting very frustrated so here I am...
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if Expression.Exec(Addresses) then begin // Get the start and length of the matching text matchStart := Expression.MatchPos[1]; matchLen := Expression.MatchLen[1]; // Surround the matching text with the HTML tags formattedText := '<b><font color="#00FF00">' + Copy(Addresses, matchStart, matchLen) + '</font></b>'; // Replace the original matching text with the formatted text using the Substitute() method formattedAddress := Expression.Substitute('<b><font color="#00FF00">$0</font></b>'); // substitute didnt work.... Sender.PostMessage(70, formattedAddress); end;
And here was another thing I tried:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- if myRegExpr.Exec(Addresses) then begin // Get the full match fullMatch := myRegExpr.Match[0]; WriteLn('full match: ' + fullMatch); //while myRegExpr.ExecNext do begin matchingText := myRegExpr.Match[1]; WriteLn('matching text: ' + matchingText); // Get the index of the capture group within the full match index := Pos(matchingText, fullMatch); // Insert the formatted text into the full match at the appropriate index formattedText := '<b><font color="#00FF00">' + matchingText + '</font></b>'; Insert(formattedText, fullMatch, index); // Replace only the matched text with the formatted text formattedAddress := StringReplace(Addresses, matchingText, formattedText, [rfReplaceAll, rfIgnoreCase]); end;
I need a little direction on this.... Thank you.
Roland57:
Hello! Here is an example.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses RegExpr; const SUBJECT = 'Free Pascal Compiler version 3.2.0 [2020/07/05] for x86_64'; var re: TRegExpr; result: string; begin re := TRegExpr.Create('\d\.\d\.\d'); result := re.Replace(SUBJECT, '<font color="red">$0</font>', TRUE); WriteLn(result); re.Free;end.
--- Quote ---Free Pascal Compiler version <font color="red">3.2.0</font> [2020/07/05] for x86_64
--- End quote ---
Leledumbo:
--- Quote from: Tony Stone on March 25, 2023, 02:43:22 am ---Now I struggle with regular expressions but the .*tony.* I understand should match "xxxxxxxxxToNyxxxxxxx" or "tonyxxxxx" ?
And lazarus.* would match "lazarusxxxxxxxxxxx" but not "LaZarusxxxxxxxxxx" ?
--- End quote ---
Your assumption is correct:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program retest; uses regexpr; const Input: array of String = ( 'xxxxxxxxxToNyxxxxxxx', 'tonyxxxxx', 'lazarusxxxxxxxxxxxxx', 'LaZarusxxxxxxxxxxxxx' );var re: TRegExpr;begin re := TRegExpr.Create; re.Expression := '.*tony.*'; re.ModifierI := true; re.InputString := Input[0]; Write(Input[0] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match'); re.InputString := Input[1]; Write(Input[1] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match'); re.Expression := 'lazarus.*'; re.ModifierI := false; re.InputString := Input[2]; Write(Input[2] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match'); re.InputString := Input[3]; Write(Input[3] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match');end.
Thaddy:
--- Quote from: Roland57 on March 25, 2023, 05:00:55 am ---
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const SUBJECT = 'Free Pascal Compiler version 3.2.0 [2020/07/05] for x86_64';
--- End quote ---
Just curious.
Are you really using such an old version or were you testing my code from an other subject?
Roland57:
It is the version shipped with with my Linux distribution, and I don't really feel the need to use a newer one. :)
Navigation
[0] Message Index
[#] Next page