Recent

Author Topic: [Solved] RegEx: Remove spaces between...  (Read 1153 times)

Espectr0

  • Full Member
  • ***
  • Posts: 235
[Solved] RegEx: Remove spaces between...
« on: September 08, 2023, 09:03:35 pm »
Hola,
I want to remove spaces between { }, and try the following code:

Code: Pascal  [Select][+][-]
  1. s := ReplaceRegExpr('\s+(?=[^{}]*\})', Text, '');
  2.  

but it tells me that identifier is not recognized in pos 14.

What am I doing wrong?
If I'm not mistaken the expression is,

Code: Pascal  [Select][+][-]
  1. \s+      # Match whitespace
  2. (?=      # only if followed by...
  3.  [^{}]*  # any number of characters except parentheses
  4.  \}      # and a closing parenthesis
  5. )        # End of lookahead assertion
  6.  

Thanks!
« Last Edit: September 10, 2023, 01:11:38 pm by Espectr0 »

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: RegEx: Remove spaces between...
« Reply #1 on: September 08, 2023, 09:10:09 pm »
Code: Pascal  [Select][+][-]
  1. uses uregexpr;
  2.  
  3. var
  4.   s: string;
  5.  
  6. begin
  7.   s := 'bla bla {    } bla bla';
  8.   writeln('s = ', s);
  9.  
  10.   s := ReplaceRegExpr('{\s+}', s, '{}');
  11.   writeln('s = ', s);
  12.  
  13.   readln;
  14. end.    

s = bla bla {    } bla bla
s = bla bla {} bla bla

This removes any white chars, if you want only spaces then:

Code: Pascal  [Select][+][-]
  1. s := ReplaceRegExpr('{ +}', s, '{}');
« Last Edit: September 08, 2023, 09:17:04 pm by Fibonacci »

Espectr0

  • Full Member
  • ***
  • Posts: 235
Re: RegEx: Remove spaces between...
« Reply #2 on: September 08, 2023, 09:16:06 pm »
and if the string is:

"{  ab c } This is a Demo { x y z }"

I want:

"{abc} This is a Demo {xyz}"

How would it be?

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: RegEx: Remove spaces between...
« Reply #3 on: September 08, 2023, 09:38:34 pm »
Code: Pascal  [Select][+][-]
  1. uses SysUtils, Classes, uregexpr;
  2.  
  3. type
  4.   tMustBeInClass = class
  5.     function regreplace(ARegExpr: TRegExpr): RegExprString;
  6.   end;
  7.  
  8. function tMustBeInClass.regreplace(ARegExpr: TRegExpr): RegExprString;
  9. var
  10.   s: string;
  11. begin
  12.   s := ARegExpr.Match[0];
  13.   s := s.Replace(' ', '');
  14.   result := s;
  15. end;
  16.  
  17. var
  18.   s, d: string;
  19.   r: TRegExpr;
  20.   m: tMustBeInClass;
  21.  
  22. begin
  23.   s := '{  ab c } This is a Demo { x y z }';
  24.   writeln('s = ', s);
  25.  
  26.   m := tMustBeInClass.Create;
  27.  
  28.   r := TRegExpr.Create('{[a-zA-Z0-9 ]*}');
  29.   d := r.Replace(s, @m.regreplace);
  30.  
  31.   writeln('d = ', d);
  32.  
  33.   readln;
  34. end.

s = {  ab c } This is a Demo { x y z }
d = {abc} This is a Demo {xyz}

That should do it. Ugly, but works.

I dont know why it wont accept just "dot" as "all chars". So you might want to add chars if needed. Other RegEx unit maybe would do it better.

EDIT: Better version

Code: Pascal  [Select][+][-]
  1. uses SysUtils, Classes, uregexpr;
  2.  
  3. type
  4.   tMustBeInClass = class
  5.     function regreplace(ARegExpr: TRegExpr): RegExprString;
  6.   end;
  7.  
  8. function tMustBeInClass.regreplace(ARegExpr: TRegExpr): RegExprString;
  9. var
  10.   s: string;
  11. begin
  12.   s := ARegExpr.Match[0];
  13.   s := s.Replace(' ', '');
  14.   result := s;
  15. end;
  16.  
  17. var
  18.   s, d: string;
  19.   r: TRegExpr;
  20.   m: tMustBeInClass;
  21.  
  22. begin
  23.   s := '{  ab c } This is a Demo { x y z } {Foo    B-  A-R     }';
  24.   writeln('s = ', s);
  25.  
  26.   m := tMustBeInClass.Create;
  27.  
  28.   r := TRegExpr.Create('({.*})');
  29.   r.ModifierG := false;
  30.   d := r.Replace(s, @m.regreplace);
  31.  
  32.   writeln('d = ', d);
  33.  
  34.   readln;
  35. end.

s = {  ab c } This is a Demo { x y z } {Foo    B-  A-R     }
d = {abc} This is a Demo {xyz} {FooB-A-R}
« Last Edit: September 08, 2023, 09:55:14 pm by Fibonacci »

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: RegEx: Remove spaces between...
« Reply #4 on: September 08, 2023, 10:10:39 pm »
Ok, final version.

Code: Pascal  [Select][+][-]
  1. uses uregexpr;
  2.  
  3. var
  4.   s: string;
  5.  
  6. begin
  7.   s := '{  ab c } This is a Demo { x y z }';
  8.   writeln('s = ', s);
  9.  
  10.   s := ReplaceRegExpr('\s+(?=[^{}]*\})', s, '', [rroModifierG]);
  11.   writeln('s = ', s);
  12.  
  13.   readln;
  14. end.

s = {  ab c } This is a Demo { x y z } {some more test} {UPPER CASE}
s = {abc} This is a Demo {xyz} {somemoretest} {UPPERCASE}

Your RegEx was fine, you just missed rroModifierG.

Espectr0

  • Full Member
  • ***
  • Posts: 235
Re: RegEx: Remove spaces between...
« Reply #5 on: September 08, 2023, 11:55:28 pm »
I get this error:

"TRegExpr compile: unrecognized modifier (pos 14)."  >:(

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: RegEx: Remove spaces between...
« Reply #6 on: September 08, 2023, 11:59:24 pm »
Maybe this?

s := ReplaceRegExpr('\s+(?=[^{}]*})', s, '', [rroModifierG]);

Or update your FPC & RTL.

Both this and previous regexes are correct:
https://regex101.com/r/KvOeTq/1

Or if this doesnt work try my "EDIT: Better version" version
« Last Edit: September 09, 2023, 12:04:55 am by Fibonacci »

Espectr0

  • Full Member
  • ***
  • Posts: 235
Re: RegEx: Remove spaces between...
« Reply #7 on: September 09, 2023, 12:05:59 am »
Same error :(
I'm using Lazarus 2.2.6, FPC 3.2.2.

Fibonacci

  • Hero Member
  • *****
  • Posts: 788
  • Internal Error Hunter
Re: RegEx: Remove spaces between...
« Reply #8 on: September 09, 2023, 12:08:24 am »
Try "Better version" from post #3.

If doesnt work update FPC to 3.3.1

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: RegEx: Remove spaces between...
« Reply #9 on: September 09, 2023, 12:20:06 am »
Indeed fpc 3.3.1 is required to let the example code from Fibonacci that uses ReplaceRegExpr work. His example from the 3th post does seem to work as intended for me with fpc 3.2.2
« Last Edit: September 09, 2023, 12:32:27 am by TRon »
Today is tomorrow's yesterday.

AlexTP

  • Hero Member
  • *****
  • Posts: 2669
    • UVviewsoft
Re: RegEx: Remove spaces between...
« Reply #10 on: September 09, 2023, 12:10:58 pm »
You can copy the file
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/regexpr/src/regexpr.pas
from FPC 3.3 Git repo to the folder of your program. And try again with new Regex.

Espectr0

  • Full Member
  • ***
  • Posts: 235
Re: [Solved] RegEx: Remove spaces between...
« Reply #11 on: September 10, 2023, 01:12:50 pm »
@Fibonacci, I will use your solution.

Thank you all!

 

TinyPortal © 2005-2018