User Tools

Site Tools


informatique:regexp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
informatique:regexp [2016/01/11 09:24] – (?= pteuinformatique:regexp [2019/09/09 08:42] – [exclure un motif] pteu
Line 1: Line 1:
 ======Expressions régulières====== ======Expressions régulières======
  
-Les expressions régulières, ou expressions rationnelles, ou motif (ou même regexp en anglais), est une suite de caractères issue de la logique (mathématique) qui permet de décrire des chaînes de caractères.+Les expressions régulières, ou expressions rationnelles, ou motif (ou même regexp en anglais), est une suite de caractères issue de la logique (mathématique) qui permet de décrire des chaînes de caractères. Il existe différentes notations, bien qu'elles se ressemblent beaucoup : POSIX, PCRE (Perl-Compatible Regular Expressions), Python, etc... [[https://fr.wikipedia.org/wiki/Expression_rationnelle#Standardisation|(plus de détail sur wikipedia)]]
  
 =====Syntaxe===== =====Syntaxe=====
Line 58: Line 58:
  
 =====Exemples===== =====Exemples=====
 +
 +  * matcher chaque ligne qui ne contient pas le MOTIF (exclure le motif)
 +<code bash>
 +^((?!MOTIF).)*$
 +
 +# alt : ne pas matcher les lignes qui commencent par MOTIF :
 +^(?!MOTIF).*$
 +</code>
 +src stackoverflow : [[https://stackoverflow.com/questions/406230/regular-expression-to-match-a-line-that-doesnt-contain-a-word|Regular expression to match a line that doesn't contain a word]]
  
   * Valider une URL :   * Valider une URL :
Line 72: Line 81:
 <code bash> <code bash>
 ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=.\-_*])([a-zA-Z0-9@#$%^&+=*.\-_]){8,}$ ^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=.\-_*])([a-zA-Z0-9@#$%^&+=*.\-_]){8,}$
 +</code>
 +
 +  * valider une adresse email selon le RFC 5322 (source : [[https://regex101.com/r/gJ7pU0/1|regex101]])
 +<code bash>
 +/(?(DEFINE)
 +    (?<addr_spec> (?&local_part) @ (?&domain) )
 +    (?<local_part> (?&dot_atom) | (?&quoted_string) | (?&obs_local_part) )
 +    (?<domain> (?&dot_atom) | (?&domain_literal) | (?&obs_domain) )
 +    (?<domain_literal> (?&CFWS)? \[ (?: (?&FWS)? (?&dtext) )* (?&FWS)? \] (?&CFWS)? )
 +    (?<dtext> [\x21-\x5a] | [\x5e-\x7e] | (?&obs_dtext) )
 +    (?<quoted_pair> \\ (?: (?&VCHAR) | (?&WSP) ) | (?&obs_qp) )
 +    (?<dot_atom> (?&CFWS)? (?&dot_atom_text) (?&CFWS)? )
 +    (?<dot_atom_text> (?&atext) (?: \. (?&atext) )* )
 +    (?<atext> [a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+ )
 +    (?<atom> (?&CFWS)? (?&atext) (?&CFWS)? )
 +    (?<word> (?&atom) | (?&quoted_string) )
 +    (?<quoted_string> (?&CFWS)? " (?: (?&FWS)? (?&qcontent) )* (?&FWS)? " (?&CFWS)? )
 +    (?<qcontent> (?&qtext) | (?&quoted_pair) )
 +    (?<qtext> \x21 | [\x23-\x5b] | [\x5d-\x7e] | (?&obs_qtext) )
 +    # comments and whitespace
 +    (?<FWS> (?: (?&WSP)* \r\n )? (?&WSP)+ | (?&obs_FWS) )
 +    (?<CFWS> (?: (?&FWS)? (?&comment) )+ (?&FWS)? | (?&FWS) )
 +    (?<comment> \( (?: (?&FWS)? (?&ccontent) )* (?&FWS)? \) )
 +    (?<ccontent> (?&ctext) | (?&quoted_pair) | (?&comment) )
 +    (?<ctext> [\x21-\x27] | [\x2a-\x5b] | [\x5d-\x7e] | (?&obs_ctext) )
 +    # obsolete tokens
 +    (?<obs_domain> (?&atom) (?: \. (?&atom) )* )
 +    (?<obs_local_part> (?&word) (?: \. (?&word) )* )
 +    (?<obs_dtext> (?&obs_NO_WS_CTL) | (?&quoted_pair) )
 +    (?<obs_qp> \\ (?: \x00 | (?&obs_NO_WS_CTL) | \n | \r ) )
 +    (?<obs_FWS> (?&WSP)+ (?: \r\n (?&WSP)+ )* )
 +    (?<obs_ctext> (?&obs_NO_WS_CTL) )
 +    (?<obs_qtext> (?&obs_NO_WS_CTL) )
 +    (?<obs_NO_WS_CTL> [\x01-\x08] | \x0b | \x0c | [\x0e-\x1f] | \x7f )
 +    # character class definitions
 +    (?<VCHAR> [\x21-\x7E] )
 +    (?<WSP> [ \t] )
 +)
 +^(?&addr_spec)$/xmg
 </code> </code>
informatique/regexp.txt · Last modified: 2020/05/24 12:02 by pteu