[英]Why MatchAgain method of TPerlRegEx doesn't works
I want to extract SAAM
and SAAMI
from the following text by using RegEX
(I'm coding in Delphi XE4 and XE5) : 我想使用
RegEX
从以下文本中提取SAAM
和SAAMI
(我在Delphi XE4和XE5中进行编码):
RegEx = 'Name:\s?(.*),Family:\s?(.*)';
For example I've this text : 例如我有这个文本:
Name: SAAM
Family: SAAMI
I wrote this code, and use the MatchAgain
method of TPerlRegEx
for matching two regex ( 'Name:\\s?(.*)'
And 'Family:\\s?(.*)'
) . 我写了这个代码,并使用
MatchAgain
的方法TPerlRegEx
用于匹配两段正则表达式( 'Name:\\s?(.*)'
和'Family:\\s?(.*)'
)。
...
var
RX: TPerlRegEx;
const
RegEx = 'Name:\s?(.*),Family:\s?(.*)';
begin
RX := TPerlRegEx.Create;
try
RX.RegEx := RegEx;
RX.Subject := mmo1.Text;// The mmo1.text value is "Name: SAAM and Family: SAAMI"
if RX.Match then
begin
repeat
ShowMessage('Name is :' + RX.Groups[1]);
ShowMessage('Family is :' + RX.Groups[2]);
until not RX.MatchAgain;
end;
finally
RX.Free;
end;
...
Why this code doesn't works ?? 为什么此代码不起作用?
I change my code to this, and it works (Almost :-) ) correctly for me. 我将代码更改为此,并且它对我来说正常工作(几乎:-))。
Example text : 示例文字:
Junk text :-)
Junk text :-)
Junk text :-)
Name: SAAM
Junk text :-)
Junk text :-)
Junk text :-)
Junk text :-)
Junk text :-)
Family: SAAMI
Junk text :-)
Junk text :-)
Junk text :-)
New code : 新代码:
var
RX: TPerlRegEx;
i: Integer;
const
RegEx = 'Name:\s?(.*)|Family:\s?(.*)';
begin
i := 1;
RX := TPerlRegEx.Create;
try
RX.RegEx := RegEx;
RX.Subject := mmo1.Text;
if RX.Match then
begin
repeat
case i of
1:
ShowMessage('Name is: ' + RX.Groups[i]);
2:
ShowMessage('Family is: ' + RX.Groups[i]);
end;
Inc(i);
until not RX.MatchAgain;
end;
finally
RX.Free;
end;
And result : 结果:
Name is: SAAM
Family is: SAAMI
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.