简体   繁体   English

为什么TPerlRegEx的MatchAgain方法不起作用

[英]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从以下文本中提取SAAMSAAMI (我在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.

相关问题 一行有效,但两行不适用于正则表达式,为什么? - One line works but two lines doesn't for regex, why? 为什么 BigQuery 不允许我的正则表达式在 Postgres 中运行? - Why doesn't BigQuery allow my regex that works in Postgres? 正则表达式适用于非数字,为什么不适用于数字? - Regex works for non digits, why it doesn't for digits? 无法弄清楚为什么我的 ROT13 转换器可以使用小写字母但不能使用大写字母 - Can't figure out why my ROT13 converter works with lowercase but it doesn't work with uppercase HTML正则表达式模式:[\\ d \\ s-] {3}有效,但[\\ d- \\ s] {3}无效。 为什么? - Html regex pattern: [\d\s-]{3} works but [\d-\s]{3} doesn't. Why? 为什么正则表达式模式可用于html注释,但不适用于php和js注释? - Why regex pattern works with html comments but doesn't work with php and js comments? split()不能正常工作 - split() doesn't works properly Delphi - TPerlRegEx / RegExBuddy问题 - Delphi - TPerlRegEx / RegExBuddy Problem 为什么正则表达式在 bash/ksh 中不起作用,而在命令行中却可以正常工作 - Why Regex doesn't work in bash/ksh while it works fine in command line 为什么我的 Python RegExp 不适用于带有进位返回的文件? - Why my Python RegExp doesn't works wih files with carry returns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM