LibPaspCre2 é Delphi e Objetos Pascal Lindings para a Biblioteca de Expressões regulares compatíveis com Perl (PCRE2). A biblioteca é um conjunto de funções que implementam a correspondência regular de padrões de expressão usando a mesma sintaxe e semântica que o Perl 5.
Biblioteca é testada para
Obtenha as fontes e adicione o diretório de origem ao caminho de pesquisa do projeto. Para FPC, adicione o diretório de origem ao arquivo fpc.cfg .
Clone o git clone https://github.com/isemenkov/libpaspcre2 .
Adicione a unidade que você deseja usar à cláusula uses .
Uma estrutura de teste consiste nos seguintes ingredientes:
unit-tests .O arquivo LIBPaspCre2.PAS contém os cabeçalhos traduzidos do PCRE2 para usar esta biblioteca em programas Pascal. Você pode encontrar a documentação da API C em www.pcre.org.
uses
libpaspcre2, utils.api.cstring;
var
re : pcre2_code_8;
rc : Integer;
pattern, subject : PCRE2_SPTR8;
error_buffer : string[ 255 ];
subject_length : Int64;
error_number : Integer;
error_offset : PCRE2_SIZE;
ovector : PPCRE2_SIZE;
match_data : ppcre2_match_data_8;
substring : string;
begin
pattern := PCRE2_SPTR8(API.CString.Create( ' (?:D|^)(5[1-5][0-9]{2}(?: |-|)[0-9]{4} ' +
' (?: |-|)[0-9]{4}(?: |-|)[0-9]{4})(?:D|$) ' ).ToPAnsiChar);
subject := PCRE2_SPTR8(API.CString.Create( ' 5111 2222 3333 4444 ' ).ToPAnsiChar);
subject_length := API.CString.Create( ' 5111 2222 3333 4444 ' ).Length;
re := pcre2_compile_8(pattern, PCRE2_ZERO_TERMINATED, 0 , @error_number, @error_offset, nil );
if re = nil then
begin
pcre2_get_error_message_8(error_number, PPCRE2_UCHAR8(@error_buffer[ 0 ]), 256 );
Fail(Format( ' PCRE2 compilation failed at offset %d: %s ' , [error_offset, error_buffer]));
end ;
match_data := pcre2_match_data_create_from_pattern_8(re, nil );
rc := pcre2_match_8(re, subject, subject_length, 0 , 0 , match_data, nil );
if rc < 0 then
begin
case rc of
PCRE2_ERROR_NOMATCH :
begin
pcre2_match_data_free_8(match_data);
pcre2_code_free_8(re);
Fail( ' No match ' );
end ;
end ;
end ;
{
ovector := pcre2_get_ovector_pointer_8(match_data);
if ovector^ > (ovector + 1)^ then
Fail('Error');
substring := Copy(PChar(subject), ovector^, (ovector + 1)^ - ovector^);
pcre2_match_data_free_8(match_data);
pcre2_code_free_8(re);
}
end ;