Recent

Author Topic: TRegExpr Help  (Read 666 times)

Tony Stone

  • Full Member
  • ***
  • Posts: 176
TRegExpr Help
« on: March 25, 2023, 02:43:22 am »
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  [Select][+][-]
  1. .*tony.* i
  2. lazarus.*

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  [Select][+][-]
  1.             if Expression.Exec(Addresses) then begin
  2.               // Get the start and length of the matching text
  3.               matchStart := Expression.MatchPos[1];
  4.               matchLen := Expression.MatchLen[1];
  5.  
  6.  
  7.               // Surround the matching text with the HTML tags
  8.               formattedText := '<b><font color="#00FF00">' + Copy(Addresses, matchStart, matchLen) + '</font></b>';
  9.  
  10.  
  11.               // Replace the original matching text with the formatted text using the Substitute() method
  12.               formattedAddress := Expression.Substitute('<b><font color="#00FF00">$0</font></b>');  // substitute didnt work....
  13.  
  14.  
  15.               Sender.PostMessage(70, formattedAddress);
  16.             end;  

And here was another thing I tried:


Code: Pascal  [Select][+][-]
  1.             if myRegExpr.Exec(Addresses) then begin
  2.               // Get the full match
  3.               fullMatch := myRegExpr.Match[0];
  4.               WriteLn('full match: ' + fullMatch);
  5.               //while myRegExpr.ExecNext do begin
  6.               matchingText := myRegExpr.Match[1];
  7.               WriteLn('matching text: ' + matchingText);
  8.               // Get the index of the capture group within the full match
  9.               index := Pos(matchingText, fullMatch);
  10.  
  11.  
  12.               // Insert the formatted text into the full match at the appropriate index
  13.               formattedText := '<b><font color="#00FF00">' + matchingText + '</font></b>';
  14.               Insert(formattedText, fullMatch, index);
  15.  
  16.  
  17.               // Replace only the matched text with the formatted text
  18.               formattedAddress := StringReplace(Addresses, matchingText, formattedText, [rfReplaceAll, rfIgnoreCase]);
  19.  
  20.  
  21.             end;


I need a little direction on this....  Thank you.
« Last Edit: March 25, 2023, 02:45:59 am by Tony Stone »

Roland57

  • Sr. Member
  • ****
  • Posts: 390
    • GitLab
Re: TRegExpr Help
« Reply #1 on: March 25, 2023, 05:00:55 am »
Hello! Here is an example.

Code: Pascal  [Select][+][-]
  1. uses
  2.   RegExpr;
  3.  
  4. const
  5.   SUBJECT = 'Free Pascal Compiler version 3.2.0 [2020/07/05] for x86_64';
  6.  
  7. var
  8.   re: TRegExpr;
  9.   result: string;
  10.  
  11. begin
  12.   re := TRegExpr.Create('\d\.\d\.\d');
  13.   result := re.Replace(SUBJECT, '<font color="red">$0</font>', TRUE);
  14.   WriteLn(result);
  15.   re.Free;
  16. end.

Quote
Free Pascal Compiler version <font color="red">3.2.0</font> [2020/07/05] for x86_64
« Last Edit: March 25, 2023, 05:02:56 am by Roland57 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8594
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRegExpr Help
« Reply #2 on: March 25, 2023, 06:52:59 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"   ?
Your assumption is correct:
Code: Pascal  [Select][+][-]
  1. program retest;
  2.  
  3. uses
  4.   regexpr;
  5.  
  6. const
  7.   Input: array of String = (
  8.     'xxxxxxxxxToNyxxxxxxx',
  9.     'tonyxxxxx',
  10.     'lazarusxxxxxxxxxxxxx',
  11.     'LaZarusxxxxxxxxxxxxx'
  12.   );
  13. var
  14.   re: TRegExpr;
  15. begin
  16.   re := TRegExpr.Create;
  17.  
  18.   re.Expression := '.*tony.*';
  19.   re.ModifierI := true;
  20.   re.InputString := Input[0];
  21.   Write(Input[0] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match');
  22.   re.InputString := Input[1];
  23.   Write(Input[1] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match');
  24.  
  25.   re.Expression := 'lazarus.*';
  26.   re.ModifierI := false;
  27.   re.InputString := Input[2];
  28.   Write(Input[2] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match');
  29.   re.InputString := Input[3];
  30.   Write(Input[3] + ' is'); if not re.Exec then Write(' not'); WriteLn(' a match');
  31. end.
  32.  

Thaddy

  • Hero Member
  • *****
  • Posts: 12971
Re: TRegExpr Help
« Reply #3 on: March 25, 2023, 09:51:08 am »
Code: Pascal  [Select][+][-]
  1. const
  2.   SUBJECT = 'Free Pascal Compiler version 3.2.0 [2020/07/05] for x86_64';
  3.  
Just curious.
Are you really using such an old version or were you testing my code from an other subject?
I actually get compliments for being rude... (well, Dutch, but that is the same)

Roland57

  • Sr. Member
  • ****
  • Posts: 390
    • GitLab
Re: TRegExpr Help
« Reply #4 on: March 25, 2023, 01:50:56 pm »
It is the version shipped with with my Linux distribution, and I don't really feel the need to use a newer one.  :)

Tony Stone

  • Full Member
  • ***
  • Posts: 176
Re: TRegExpr Help
« Reply #5 on: March 25, 2023, 02:30:38 pm »
I guess I should clarify what I am trying to do.


  • User enters a list regular expressions.
  • The users expressions are only to find match of a pattern in a massive list of addresses
  • My Pascal program creates a tregexpr for each user entered expression
  • The program then iterates a massive list of data trying to match users expressions
  • On a match I just want to make ONLY the matched part of the data bold in green with HTML
  • The large list of data is addresses so the users expressions can never alter the characters of addresses.
  • Only my loop executing the tregexpr can add HTML characters to surround the text that matched the users expression.
When I get back on the computer later I will show you the final code I came up with last night.  It still was wrong but I think I started making progress and the HTML tag was surrounding the very beging of the input string all the way to the end of the matching part of the string.  So I was getting close.  I would like to make it work using the example expressions I have in my post.  Once that works I'm gonna start testing other expressions.
« Last Edit: March 25, 2023, 06:32:26 pm by Tony Stone »

Tony Stone

  • Full Member
  • ***
  • Posts: 176
Re: TRegExpr Help
« Reply #6 on: March 25, 2023, 06:30:08 pm »
So this is my current *working* code.  It works if the user entered simply `lazarus` and the Addresses is `X45lazarusEGCDQqLE8cS9uEwyuYYP5PmF`.  It puts the green font and bold around `lazarus` which is what I want.


Now if the user were to enter `.{3}lazarus` it matches 3 characters before `lazarus` as well and surrounds it with the HTML(not really what I want).  So I guess my code DOES work but the issue is gonna be relying on the user to enter a proper regular expression.  So I am open to ideas... The only thing my user should be trying to enter as expressions would be something to find a matching string within the generated addresses.  I probably need to re-think all of this.  The end goal is to let the user find a "vanity" address based on their input and I want to essentially highlight the matching part of their expression.

I'm sorry if I am not explaining this well... I am having a hard time in part because I am not very good with the Rgular Expressions and maybe what I am seeing is completeley expected based on the expressions I am trying.



Code: Pascal  [Select][+][-]
  1.         for i := 0 to High(Addresses) do
  2.         begin
  3.           for V := 0 to High(vanityExpression) do
  4.           begin
  5.             if vanityExpression[V].Exec(Addresses[i]) then
  6.             begin
  7.               // Surround the matching text with the HTML tags
  8.               matchingText := vanityExpression[V].Match[0];
  9.               WriteLn(matchingText);
  10.               formattedText := '<b><font color="#00FF00">' + matchingText + '</font></b>';
  11.  
  12.  
  13.               // Replace the matching text with the formatted text
  14.               formattedAddress := Addresses[i];
  15.               System.Delete(formattedAddress, vanityExpression[V].MatchPos[0], vanityExpression[V].MatchLen[0]);
  16.               System.Insert(formattedText, formattedAddress, vanityExpression[V].MatchPos[0]);
  17.  
  18.  
  19.               Sender.PostMessage(70, 'VANITY: ' + formattedAddress + ' ' + randKeyHexStr);
  20.             end;
  21.           end;
  22.  
« Last Edit: March 25, 2023, 06:32:50 pm by Tony Stone »

Tony Stone

  • Full Member
  • ***
  • Posts: 176
Re: TRegExpr Help
« Reply #7 on: March 25, 2023, 06:36:38 pm »
This is just to show what I am shooting for if it gives some context


Leledumbo

  • Hero Member
  • *****
  • Posts: 8594
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRegExpr Help
« Reply #8 on: March 29, 2023, 09:32:04 am »
Now if the user were to enter `.{3}lazarus` it matches 3 characters before `lazarus` as well and surrounds it with the HTML(not really what I want)
But it's a valid regex for any 3 preceeding characters.

Tony Stone

  • Full Member
  • ***
  • Posts: 176
Re: TRegExpr Help
« Reply #9 on: March 29, 2023, 12:47:52 pm »
Now if the user were to enter `.{3}lazarus` it matches 3 characters before `lazarus` as well and surrounds it with the HTML(not really what I want)
But it's a valid regex for any 3 preceeding characters.


 I wasn't being clear and correct.  .3{ton} it surrounds the entire string in HTML... Not just the matching part of the string.  Not sure if it's my code that is the issue or the regex library or that expression itself.  The screenshot I posted didn't show this situation but I was just trying to clarify the intent of my code with the screenshot


Leledumbo

  • Hero Member
  • *****
  • Posts: 8594
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TRegExpr Help
« Reply #10 on: March 30, 2023, 09:12:33 am »
I wasn't being clear and correct.  .3{ton} it surrounds the entire string in HTML... Not just the matching part of the string.  Not sure if it's my code that is the issue or the regex library or that expression itself.  The screenshot I posted didn't show this situation but I was just trying to clarify the intent of my code with the screenshot
Modify my simple, fully compilable program above to demonstrate what you mean. I still don't get it as looking at your code, you did WriteLn() the matchingtext, so you should be able to see what it captured, and the following program proves it so:
Code: Pascal  [Select][+][-]
  1. program retest;
  2.  
  3. uses
  4.   regexpr;
  5.  
  6. const
  7.   Subjects: array of String = (
  8.     'lazarus',
  9.     'alazarus',
  10.     'abclazarus',
  11.     'somethingsolongwithlazarusinbetween'
  12.   );
  13. var
  14.   re: TRegExpr;
  15.   i: String;
  16.   Matched: Boolean;
  17. begin
  18.   re := TRegExpr.Create;
  19.  
  20.   re.Expression := '.{3}lazarus';
  21.   for i in Subjects do begin
  22.     re.InputString := i;
  23.     Matched := re.Exec;
  24.     Write(i + ' is'); if not Matched then Write(' not'); WriteLn(' a match');
  25.     if Matched then WriteLn('Matched string: ',re.Match[0]);
  26.     WriteLn;
  27.   end;
  28. end.
  29.  

 

TinyPortal © 2005-2018