std :: regex_match y cuantificador perezoso con comportamiento extraño
Yo sé eso:
Coincidencias de cuantificador diferido: lo menos posible (coincidencia más corta)
También sepa que el constructor:
basic_regex( ...,
flag_type f = std::regex_constants::ECMAScript );
Y:ECMAScript
apoyano codicioso partidos,
y elECMAScript
regex"<tag[^>]*>.*?</tag>"
coincidiría solo hasta queprimero etiqueta de cierre ...en.cppreference
Y:
Como máximo, se debe elegir una opción de gramática deECMAScript
, basic
, extended
, awk
, grep
, egrep
. Si no se elige gramática,ECMAScript
se supone que está seleccionado ...en.cppreference
Y:
Tenga en cuenta queregex_match
solo coincidirá con éxito una expresión regular con una secuencia de caracteres completa, mientras questd::regex_search
coincidirá con éxito subsecuencias ...std :: regex_match
Aquí está mi código: +En Vivo
#include <iostream>
#include <string>
#include <regex>
int main(){
std::string string( "s/one/two/three/four/five/six/g" );
std::match_results< std::string::const_iterator > match;
std::basic_regex< char > regex ( "s?/.+?/g?" ); // non-greedy
bool test = false;
using namespace std::regex_constants;
// okay recognize the lazy operator .+?
test = std::regex_search( string, match, regex );
std::cout << test << '\n';
std::cout << match.str() << '\n';
// does not recognize the lazy operator .+?
test = std::regex_match( string, match, regex, match_not_bol | match_not_eol );
std::cout << test << '\n';
std::cout << match.str() << '\n';
}
y la salida:
1
s/one/
1
s/one/two/three/four/five/six/g
Process returned 0 (0x0) execution time : 0.008 s
Press ENTER to continue.
std::regex_match
no debe coincidir con nada y debe volver0
conno codicioso cuantificador.+?
De hecho, aquí, elno codicioso .+?
cuantificador tiene el mismo significado quecodicioso uno y ambos/.+?/
y/.+/
coincidir con la misma cadena. Son diferentes patrones.Entonces, el problema es por qué se ignora el signo de interrogación.
Prueba rápida:
$ echo 's/one/two/three/four/five/six/g' | perl -lne '/s?\/.+?\/g?/ && print $ echo 's/one/two/three/four/five/six/g' | perl -lne '/s?\/.+?\/g?/ && print $&'
$ s/one/
$
$ echo 's/one/two/three/four/five/six/g' | perl -lne '/s?\/.+\/g?/ && print $&'
$ s/one/two/three/four/five/six/g
amp;'
$ s/one/
$
$ echo 's/one/two/three/four/five/six/g' | perl -lne '/s?\/.+\/g?/ && print $ echo 's/one/two/three/four/five/six/g' | perl -lne '/s?\/.+?\/g?/ && print $&'
$ s/one/
$
$ echo 's/one/two/three/four/five/six/g' | perl -lne '/s?\/.+\/g?/ && print $&'
$ s/one/two/three/four/five/six/g
amp;'
$ s/one/two/three/four/five/six/g
NOTA
esta expresión regular:std::basic_regex< char > regex ( "s?/.+?/g?" );
no codicioso
y esto :std::basic_regex< char > regex ( "s?/.+/g?" );
codicioso
tener la misma salida constd::regex_match
. ¡Aún así ambos coinciden con toda la cadena!
Pero constd::regex_search
Tener la salida diferente.
tambiéns?
og?
no importa y con/.*?/
todavía coincide con toda la cadena!
Mas detalle
g++ --version
g++ (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901