Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
General
»
Regex
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
open dialog - default dir...
by
Nicole
[
Today
at 08:02:40 pm]
System.Now gives timestam...
by
Thaddy
[
Today
at 07:59:53 pm]
Installing and using Vamp...
by
Odacir
[
Today
at 07:58:22 pm]
Multidimensional arrays a...
by
Kays
[
Today
at 07:36:57 pm]
Demo Scene Picture sinwav...
by
circular
[
Today
at 07:27:02 pm]
Demo Scene Bitmap Font Sc...
by
KodeZwerg
[
Today
at 07:21:14 pm]
what's difference between...
by
ASerge
[
Today
at 07:09:52 pm]
splitting an image
by
KodeZwerg
[
Today
at 07:07:21 pm]
Debugger error
by
bobonwhidbey
[
Today
at 07:03:07 pm]
How to: create DLL file f...
by
paule32
[
Today
at 07:02:31 pm]
Your best UI design - con...
by
circular
[
Today
at 06:48:12 pm]
Web Applications with Pas...
by
1HuntnMan
[
Today
at 06:44:00 pm]
Converting a string/index...
by
1HuntnMan
[
Today
at 06:35:19 pm]
Drawing grid lines on a t...
by
KodeZwerg
[
Today
at 05:42:19 pm]
Compile/Convert Delphi pr...
by
Martin_fr
[
Today
at 05:29:03 pm]
Lazarus features in a non...
by
Lutz Mändle
[
Today
at 05:26:34 pm]
Lazarus for Windows on aa...
by
msintle
[
Today
at 05:03:57 pm]
AI, NLP and CAI: Text Gen...
by
indydev
[
Today
at 04:51:00 pm]
Parameter passing odditie...
by
KodeZwerg
[
Today
at 03:41:49 pm]
Surf Units in Text Mode I...
by
mosquito
[
Today
at 01:58:21 pm]
Ho Ho Ho IntraWeb in Laza...
by
Thaddy
[
Today
at 01:55:52 pm]
Generics - correct syntax
by
Thaddy
[
Today
at 01:48:32 pm]
FpDebug questions
by
440bx
[
Today
at 01:22:53 pm]
leetcode doesn’t support ...
by
codeninja
[
Today
at 01:16:43 pm]
Can function be used for ...
by
Joanna
[
Today
at 12:48:19 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Regex (Read 999 times)
BubikolRamios
Sr. Member
Posts: 267
Regex
«
on:
June 16, 2019, 07:45:30 am »
Can't see why this regex is not found ?
Code: Pascal
[Select]
[+]
[-]
var
re
:
TRegExpr
;
begin
re
:
=
TRegExpr
.
Create
(
'<foo>foo</foo>'
)
;
if
re
.
Exec
(
'>.*?<'
)
then
begin
WriteLn
(
re
.
Match
[
1
]
)
;
while
re
.
ExecNext
do
begin
WriteLn
(
re
.
Match
[
1
]
)
;
end
;
end
;
re
.
Free
;
end
;
Logged
lazarus 3.2-fpc-3.2.2-win32/win64
trev
Global Moderator
Hero Member
Posts: 2023
Former Delphi 1-7, 10.2 user
Re: Regex
«
Reply #1 on:
June 16, 2019, 09:10:20 am »
Your syntax needs help
Try:
Code: Pascal
[Select]
[+]
[-]
re
:
=
TRegExpr
.
Create
;
re
.
Expression
:
=
'>.*?<'
;
if
re
.
Exec
(
'<foo>foo</foo>'
)
then
begin
WriteLn
(
re
.
Match
[
0
]
)
;
while
re
.
ExecNext
do
begin
WriteLn
(
re
.
Match
[
1
]
)
;
end
;
end
;
re
.
Free
;
«
Last Edit: June 16, 2019, 09:11:51 am by trev
»
Logged
Thaddy
Hero Member
Posts: 14392
Sensorship about opinions does not belong here.
Re: Regex
«
Reply #2 on:
June 16, 2019, 09:26:35 am »
Also not correct, try this and a bit shorter (except the try/finally I always add):
Code: Pascal
[Select]
[+]
[-]
{$mode objfpc}
uses
regexpr
;
var
re
:
TRegExpr
;
begin
re
:
=
TRegExpr
.
Create
(
'>.*?<'
)
;
// NOT the string to search, but the expression
try
if
re
.
Exec
(
'<foo>foo</foo>bar<foo>'
)
then
// Here goes the string to process, not the expression.
begin
WriteLn
(
re
.
Match
[
0
]
)
;
// index starts at zero, not one...
while
re
.
ExecNext
do
// to test the second match I added...
WriteLn
(
re
.
Match
[
0
]
)
;
end
;
finally
re
.
Free
;
end
;
end
.
>If you don't mind
with
, even:
Code: Pascal
[Select]
[+]
[-]
{$mode objfpc}
uses
regexpr
;
begin
with
TRegExpr
.
Create
(
'>.*?<'
)
do
// NOT the string to search, but the expression
try
if
Exec
(
'<foo>foo</foo>bar<foo>'
)
then
// Here goes the string to process, not the expression.
begin
WriteLn
(
Match
[
0
]
)
;
// index starts at zero, not one...
while
ExecNext
do
// to test the second match I added...
WriteLn
(
Match
[
0
]
)
;
// index is still zero, not one or higher
end
;
finally
Free
;
end
;
end
.
This is all well explained in the sourcecode.
«
Last Edit: June 16, 2019, 10:33:55 am by Thaddy
»
Logged
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.
BubikolRamios
Sr. Member
Posts: 267
Re: Regex
«
Reply #3 on:
June 16, 2019, 10:20:10 am »
Thanks.
Logged
lazarus 3.2-fpc-3.2.2-win32/win64
Thaddy
Hero Member
Posts: 14392
Sensorship about opinions does not belong here.
Re: Regex
«
Reply #4 on:
June 16, 2019, 10:47:38 am »
Maybe I should add that the match list is there for subexpressions. If you do not use them, always read from zero.
Logged
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
General
»
Regex
TinyPortal
© 2005-2018