LibpaSpCre2 es Delphi y Object Pascal Bindings para la biblioteca de expresiones regulares compatibles con PERL (PCRE2). La biblioteca es un conjunto de funciones que implementan la coincidencia de patrones de expresión regulares utilizando la misma sintaxis y semántica que PERL 5.
La biblioteca se prueba para
Obtenga las fuentes y agregue el directorio de origen a la ruta de búsqueda del proyecto. Para FPC, agregue el directorio de origen al archivo fpc.cfg .
Clone El repositorio git clone https://github.com/isemenkov/libpaspcre2 .
Agregue la unidad que desea usar a la cláusula uses .
Un marco de prueba consta de los siguientes ingredientes:
unit-tests .LibpaSpCre2.pas El archivo contiene los encabezados traducidos PCRE2 para usar esta biblioteca en programas Pascal. Puede encontrar la documentación de C API en el sitio web 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 ;