regex - Qu'est-ce qu'un groupe non captivant dans des expressions régulières

Mots clés : regexcapturing-groupregex-groupregex

meilleur 4 Réponses regex - Qu'est-ce qu'un groupe non captivant dans des expressions régulières

vote vote

96

http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex 
(https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? 
Match "http://stackoverflow.com/"      Group 1: "http"      Group 2: "stackoverflow.com"      Group 3: "/"  Match "https://stackoverflow.com/questions/tagged/regex"      Group 1: "https"      Group 2: "stackoverflow.com"      Group 3: "/questions/tagged/regex" 
(?:https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? 
Match "http://stackoverflow.com/"      Group 1: "stackoverflow.com"      Group 2: "/"  Match "https://stackoverflow.com/questions/tagged/regex"      Group 1: "stackoverflow.com"      Group 2: "/questions/tagged/regex" 
   \<(?<TAG>.+?)\> [^<]*? \</\k<TAG>\> or    \<(.+?)\> [^<]*? \</\1\> 
Lorem ipsum dolor sit amet consectetuer feugiat fames malesuada pretium egestas. 
\b(\S)(\S)(\S)(\S*)\b 
Match "Lorem"      Group 1: "L"      Group 2: "o"      Group 3: "r"      Group 4: "em" Match "ipsum"      Group 1: "i"      Group 2: "p"      Group 3: "s"      Group 4: "um" ...  Match "consectetuer"      Group 1: "c"      Group 2: "o"      Group 3: "n"      Group 4: "sectetuer" ... 
$1_$3$2_$4 
L_ro_em i_sp_um d_lo_or s_ti_ a_em_t c_no_sectetuer f_ue_giat f_ma_es m_la_esuada p_er_tium e_eg_stas. 
vote vote

84

([0-9]+)(?:st|nd|rd|th)? 
vote vote

76

/(?:\d{1,3}\.){3}\d{1,3}/ 
vote vote

62

"abc".match(/(.)(.)./).captures #=> ["a","b"] "abc".match(/(?:.)(.)./).captures #=> ["b"] 

Questions similaires