Recent

Author Topic: How to replace all matches of regex in a string?  (Read 6067 times)

bulrush

  • Jr. Member
  • **
  • Posts: 84
How to replace all matches of regex in a string?
« on: June 07, 2016, 08:47:27 pm »
I've searched Google and these forums, found some documentation on TRegExpr, and still haven't found an example of how to do this.

Basically I want to change this: <http://www.google.com>, <http://www.facebook.com>

Into HTML like this: <a href="http://www.google.com">http://www.google.com</a>, <a href="http://www.facebook.com">http://www.facebook.com</a>

That is, I want to replace all instances of a match. Here's my code so far.
Code: Pascal  [Select][+][-]
  1. Program test;
  2. uses Regexpr;
  3.  
  4. var tre,ts:string;
  5.   Regex:TRegexpr;
  6.   done:boolean;
  7.  
  8. begin
  9. tre:='<(http://|ftp://|mailto:).+>';
  10. Regex.Expression :=tre ;
  11. //if Regex.Exec(ts) then
  12. done:=false;
  13. while (not done) do
  14.   if Regex.Exec(ts) then
  15.     ts:=ReplaceRegExpr(tre, ts, '<a href="$&">$&</a>', True)
  16.   else
  17.       done:=true;
  18.  
  19. writeln('TS='+ts);
  20.  
  21. end. // End program
  22.  

Surely someone has done this before. In Perl I use the 'g' modifier.

Thank you!
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: How to replace all matches of regex in a string?
« Reply #1 on: June 08, 2016, 05:13:15 am »
Basically I want to change this: <http://www.google.com>, <http://www.facebook.com>

Into HTML like this: <a href="http://www.google.com">http://www.google.com</a>, <a href="http://www.facebook.com">http://www.facebook.com</a>

Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   Regexpr;
  6.  
  7. var
  8.   tre, ts: string;
  9.  
  10. begin
  11.   tre := '<((http://|ftp://|mailto:)[^>]+)>';
  12.   ts := '<http://www.google.com>, <http://www.facebook.com>';
  13.   ts := ReplaceRegExpr(tre, ts, '<a href="$1">$1</a>', True);
  14.   WriteLn(ts);
  15.   ReadLn;
  16. end.
  17.  

Quote
In Perl I use the 'g' modifier.

Code: Pascal  [Select][+][-]
  1.   Regex.ModifierG := True OR False;
  2.  

bulrush

  • Jr. Member
  • **
  • Posts: 84
Re: How to replace all matches of regex in a string?
« Reply #2 on: June 08, 2016, 03:14:51 pm »
Thank you. You added the exclusion of the greater than > sign at the end of the search expression. That made it work.

Thank you for your patience. With every question I learn more.

I also remembered to test regexes at https://regex101.com/.
« Last Edit: June 08, 2016, 03:20:43 pm by bulrush »
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

bulrush

  • Jr. Member
  • **
  • Posts: 84
Re: How to replace all matches of regex in a string?
« Reply #3 on: June 08, 2016, 03:35:36 pm »
Ok, I have a similar regex that won't work, and I even looked at the ending characters to exclude. Try it out here: https://regex101.com/r/eK2eT6/1

I want to change: '[Another link to Google](http://www.google.com), [a secure link to Google](https://www.google.com),
[Bad link to Google](www.google.com)'

to:

<a href="http://www.google.com">Another link to Google</a>, <a href="https://www.google.com">a secure link to Google</a>,
[Bad link to Google](www.google.com)

Notice the last link is not converted.

EDIT: Got it! I knew my regex was wrong somewhere.
Code: Pascal  [Select][+][-]
  1. tre:='\[(.+?)\]\(((https?|ftp|mailto).+?\))';
  2. ts := '[Link to Google](http://www.google.com), '+
  3. '[a secure link to Google](https://www.google.com),'+
  4. '[Bad link to Google](www.google.com)';
  5. writeln('Input is:'+ts);
  6. ts := ReplaceRegExpr(tre, ts, '<a href="$2">$1</a>', True);
  7. writeln('Result:',ts);
  8.  
« Last Edit: June 08, 2016, 04:31:13 pm by bulrush »
Lazarus 1.6.0, FPC 3.0.0, Win 7 64-bit, current Perl programmer.

Thaddy

  • Hero Member
  • *****
  • Posts: 10573
Re: How to replace all matches of regex in a string?
« Reply #4 on: November 05, 2019, 11:50:41 am »
Use an escape, i.e \\

You asked or re-opened this today but I got this: see screenshot.

maurobio

  • Full Member
  • ***
  • Posts: 156
Re: How to replace all matches of regex in a string?
« Reply #5 on: November 05, 2019, 12:11:07 pm »
Thanks, @Thaddy!

I deleted my post because I just found a more recent thread and then posted it there (to which you also kindly answered!). I just got concerned that the admins might complain about cross-postings...  :-[

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.8 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

 

TinyPortal © 2005-2018