### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Regex question  (Read 4818 times)

#### ASerge

• Hero Member
• Posts: 1671
##### Re: Regex question
« Reply #45 on: October 22, 2019, 11:04:31 pm »
I want to find all occurences of a pattern in a text.
Requirements: follows the nothing= and has to be within brackets.
Code: Pascal  [Select][+][-]
1. {\$MODE OBJFPC}
2. {\$APPTYPE CONSOLE}
3. {\$LONGSTRINGS ON}
4.
5. uses RegExpr;
6.
7. procedure Test(const S: string);
8. var
9.   ROut, RIn: TRegExpr;
10. begin
11.   ROut := TRegExpr.Create('\(([^\)]*)\)');
12.   try
13.     if ROut.Exec(S) then
14.     begin
15.       RIn := TRegExpr.Create('nothing="([^"]+)"');
16.       try
17.         repeat
18.           if RIn.Exec(ROut.Match[1]) then
19.             repeat
20.               Writeln(RIn.Match[1]);
21.             until not RIn.ExecNext;
22.         until not ROut.ExecNext;
23.       finally
24.         RIn.Free;
25.       end;
26.     end;
27.   finally
28.     ROut.Free;
29.   end;
30. end;
31.
32. const
33.   CSampleInputText =
34.     'Something (anything nothing="hey!", anything something, nothing="hola!", thing)' + LineEnding +
35.     'something nothing="aloha!"' + LineEnding +
36.     '(skip empty nothing="")' + LineEnding +
37.     '(other nothing="other!", nothing="again!", thing)';
38. begin
39.   Test(CSampleInputText);
41. end.

#### maurobio

• Full Member
• Posts: 156
##### Re: Regex question
« Reply #46 on: November 05, 2019, 11:46:14 am »
Dear ALL,

I have strings with some embedded characters as follows:

s := '\i{}This is a string\i0{}'

I want to get rid of the \{} and \i0{} using the following regex:

e := '\\i\d*{}'

But when I attempt this:

n := ReplaceRegExpr(e, s, '', True);

I get the error:

TRegrExpr(comp): Nested *?+ (pos 6)

The above regex works in Python (as n = re.sub(e, '', s)), but obviously the FPC implementation is different.

Could someone give me a hand?

Best regards,
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

• Hero Member
• Posts: 10481
##### Re: Regex question
« Reply #47 on: November 05, 2019, 11:51:45 am »
I already replied in the other post: use an escape to escape a slash \\
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

#### maurobio

• Full Member
• Posts: 156
##### Re: Regex question
« Reply #48 on: November 05, 2019, 12:11:38 pm »
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

#### maurobio

• Full Member
• Posts: 156
##### Re: Regex question
« Reply #49 on: November 05, 2019, 12:18:40 pm »
... but when I changed the regex to:

e := '\\i\\d*{}'

I still get the same error (just one position changed):

TRegrExpr(comp): Nested *?+ (pos 7)

Any hints?
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

#### bytebites

• Sr. Member
• Posts: 299
##### Re: Regex question
« Reply #50 on: November 05, 2019, 01:44:02 pm »
e:='\\i\d*\{}'

() is not needed.
« Last Edit: November 05, 2019, 01:57:21 pm by bytebites »

#### maurobio

• Full Member
• Posts: 156
##### Re: Regex question
« Reply #51 on: November 05, 2019, 09:03:23 pm »
@bitebyes:

There are no parentheses ('()') in my regex, those are braces ('{}'), which are part of the string and that I want to get rid of.

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