How to get named regular expression groups in Delphi?

I am trying to use regex in Delphi to regex HTML and retrieve some data.

My goal is to create a query string followed by sintax:

?namedGroup1=valueNamedGroup1&namedGroup2=valueNamedGroup2

I have n Regular Expression Array:

array[0] = '<div (id="(?<id>[a-zA-Z0-9]+)"|name="(?<name>[a-zA-Z0-9]+))"';

My html:

<h1>bla bla bla</h1> <div id="home">

If I applied this regular expression using the built-in regular expression in PHP, it will return an associative array

RegArray[0] = '<div id="home">'
RegArray['id'] = 'home'

if I do foreach, I easily get a list of named groups, and I can create my query:

?id=home

The thing is, I don’t know if the regular expression will match the named group identifier or name, and I need to know that.

Delphi returns only a simple array

RegArray[0] = '<div id="home">'
RegArray[1] = 'home'  // ID or NAME?

So, how do I get the named group and the named value of Group?

here it is my code:

var RegEx: TRegEx;
begin
 RegEx := TRegEx.Create(array[0], [roIgnoreCase,roMultiline]);
 Match := RegEx.Match(html);
 if (Match.Success) then
 begin
   //get the group here.
 end;

: http://www.regular-expressions.info/delphi.html

+4
3

, : - ! , - PHP; -)

  • : <div (id="(?<id>[a-zA-Z0-9]+)"|name="(?<name>[a-zA-Z0-9]+))"
  • mine: <div (id="(?<id>[a-zA-Z0-9]+)"|name="(?<name>[a-zA-Z0-9]+)")

DI RegExp demo

pcre.org engine + http://www.yunqa.de/delphi/doku.php/products/regex/index


: http://www.regular-expressions.info/delphi.html

, RegEx: http://www.regexbuddy.com/test.html

, ...


, , - HTML- . HTML,

 <!-- <p><div name="bla-bla"> ... </div></p> -->

 <img src="...." alt='Press to insert <div id="123"> to you sample text' />

 <DIV ID="my cool id" />

, .

Regex,

, , . ( ... " ... ) .... " - !

- Delphi.

Delphi . / . . , , .

PHP ,

, PHP ( PHP-), " PHP"

Delphi, PHP-.

array[0] = '<div (id="(?<id>[a-zA-Z0-9]+)"|name="(?<name>[a-zA-Z0-9]+))"'; - , .

, PHP Delphi . .

, DELPHI

  • , . Delphi - , RegEx.
  • Delphi- PCRE engine - . , . Delphi <name, value>.

, HTML,

HTML, .

+2

,

, Delphi.

PHP , , DELPHI , , , .

, HTML, , Delphi.

0

, , :

const
  cRegEx = '<div (id="(?<id>[a-zA-Z0-9]+)"|name="(?<name>[a-zA-Z0-9]+)")';
  cHtml = '<h1>bla bla bla</h1> <div id="home">';
var
  group: TGroup;
  match: TMatch;
  regEx: TRegEx;
begin
  regEx := TRegEx.Create(cRegEx, [roIgnoreCase,roMultiline]);
  match := regEx.Match(cHtml);
  if match.Success then begin
    group := match.Groups['id'];
    Assert(group.Value = 'home');
  end;
end;
0

All Articles