Expresiones regulares (regexp): conceptos básicos

Las expresiones regulares (también llamadas regexp o regex ) son un mecanismo para buscar y reemplazar texto. En una línea, en un archivo, en varios archivos ... ¡Los desarrolladores los usan en el código de la aplicación, los probadores en los autotests o simplemente cuando trabajan en la línea de comandos!





¿Por qué es mejor que una simple búsqueda? El que te permite configurar la plantilla.





Por ejemplo, la fecha de nacimiento llega a la entrada con el formato DD.MM.YYYYY. Necesita transferirlo más, pero en el formato AAAA-MM-DD. ¿Cómo puedo hacer esto con una simple búsqueda? No sabes de antemano cuál será la fecha.





« - ».





?





  1. , test ( )









  2. grep-









  3. ...





— , . , . 200, . ( Notepad++ ).





, . .

































  1. ( )





























. , — , , , . :





  1. Notepad++ ( Search Mode → Regular expression)





  2. Regex101 ( )





  3. Myregexp





  4. Regexr





,





. —  , .





: , ,





Regex:





: , ,





, regex, . BACKGROUND-COLOR , ( ) , , regex :





, «», «». !





, , . JavaScript i, . (notepad++) «Match case». , . , , .





, ?





: , , ,





Regex:





: , , ,





. JavaScript g (global), , .





, , ? :





: , 55,





Regex:





: , 55,





. . , , , - . - ... ? . ?





, «». , , :





Regex:





:

















, . . , , . , .





 





 





. — ().





:

























Regex: .





:

























El símbolo "."  reemplaza cualquier 1 carácter
«.» 1

, , , . , :





6





&









! , — , . , ? ?





!





Regex: file.





:





file.txt





file1.txt





file2.xls





, ? , txt :





Regex: .txt





:





file.txt





log.txt





file.png





1txt.doc





one_txt.jpg





, txt , «» , «txt» . , ( ).





, —  :





Regex: \.txt





:





file.txt





log.txt





file.png





1txt.doc





one_txt.jpg





. ? .





:





. —





\. —





 





, «», «» . , , :





Regex: ..





:













74









^&









, . , :





Regex: [][]





:













74









^&









! , «», .





? . , :





[] — «» «»





[-] — «» «» ( «»)





[-]    —





[--]  —





[a-z]  —





[a-zA-Z]  —





[0-9]  —





[-]   — «» «» (, —  )





[--]   — «» «» «» «»





— , ! , — .





[] — «», «» «»





[ ] — «», «», «», ( )





[, , ] — «», «», «»,





—  . — , :





  • — 





  • — 





! , ! , - [1-31]. , 1 31, :





  • 1 3





  • 1





. , 1 31! . , , . ! , ! , - =)) ...





:





Regex: . [-]





:





















«.»:





6





&









^ [] :





[^0-9]  — ,





[^]  — , «»





[^-8]  — , «», «», «» 8





, txt , — :





Regex: [^0-9]\.txt





:





file.txt





log.txt





file_1.txt





1.txt





, :





Regex: fruits[0]





: fruits0





: fruits[0]





« «fruits», 0». — , .





0- , :





Regex: fruits\[0\]





: fruits[0]





: fruits0





, !





Regex: fruits\[[0-9]\]





:





fruits[0] = “”;





fruits[1] = “”;





fruits[2] = “”;





:





cat[0] = “ ”;





, «» , ...





! , . -? .





, . :





— , !





, . , . ... , . , , .





« », ( —  , ...), , !





— fruits\[[0-9]\]





— «fruits».





. , - .





? . , — «fruits[»





. — , . .





. : [0-9]. . . 10, 11 325, ( ) .





: fruits[« »





. .





— ]





: fruits[« »]





! , , ... :





Regex: fruits\[[0-9]\]





:





fruits[0] = “”;





fruits[1] = “”;





fruits[9] = “”;





:





fruits[10] = “”;





fruits[325] = “ ”;





, , «».





, .





? ..:





  • 2









  • 2









  • 4





: [0-9][0-9]\.[0-9][0-9]\.[0-9][0-9][0-9][0-9].





, [1-31]. « 1 31», « 1 3, 1». .





, . , ? regexp?





! 8888 99 , ?





Regex: [0-9][0-9]\.[0-9][0-9]\.[0-9][0-9][0-9][0-9]





:





01.01.1999





05.08.2015





:





08.08.8888





99.99.2000





:





  • 31 — [0-3]





  • 12 —  [01]





  • 19.., 20.. — [12], [09]





, , . , , , , «9999» «99» ...





, :





Regex: [0-3][0-9]\.[0-1][0-9]\.[12][09][0-9][0-9]





:





08.08.8888





99.99.2000





:





33.01.2000





01.19.1999





05.06.2999





. 31 , 39. , . , .





 





[] . , — |.





Regex: ||





:

















:













. — 





Regex: (|)





:













. «», «». ? , . —  «», «»:





Regex: |





:





















«» «», . .





2 :





  • (|)





  • []





[], , .





« ». [0-3][0-9], 33, 35, 39... !





. ---... :





  • 0 — 1 9 ( 00 )





  • 1, 2 — 0 9





  • 3 — 0 1





:





  • 0[1-9]





  • [12][0-9]





  • 3[01]





! : 0[1-9]|[12][0-9]|3[01]





. =)





, , , :





(<>)\.(<>)\.(<>)





— . ? , . , , :





[0-1][0-9]\.[12][09][0-9][0-9]





, :





0[1-9]|[12][0-9]|3[01]\.[0-1][0-9]\.[12][09][0-9][0-9]





?





  •    0[1-9]





  •    [12][0-9]





  •     3[01]\.[0-1][0-9]\.[12][09][0-9][0-9]





? «19» . , | . , . , .





—  , !





Regex: (|||)





:





















:





Regex: |||





:





















, :





  • —  []





  • — |









, [0-9].





, [--a-zA-Z].





?





! , :

















\d





[0-9]









\D





[^0-9]









\s





[ \f\n\r\t\v]









\S





[^ \f\n\r\t\v]









\w





[[:word:]]









\W





[^[:word:]]





,





.





 









 





, . «». \d — . « »? :













 









\r





(Carriage return, CR)





\n





(Line feed, LF)





\t





(Tab)





\v





(vertical tab)





\f





(Form feed)





[\b]





1 (Backspace)





 





—  «\r\n». :













:





\r\n





backspace ? ? . ! - ? , —  , [\b]?





—  . backspace — ASCII , (ASCII code 8, 10 octal). «» , ( JavaScript):





console.log("abc\b\bdef");
      
      



:





adef
      
      



«abc», «b» «». , . . , .   [\b] .





:





What's the use of the [\b] backspace regex? —





, \s, , , .





, . [[:word:]]? . , , . :













[[:alnum:]]





: [--a-zA-Z0-9]





[[:alpha:]]





: [--a-zA-Z]





[[:digit:]]





: [0-9]





[[:graph:]]





(, . . )





[[:print:]]









[[:space:]]





[ \f\n\r\t\v]





[[:punct:]]





: ! " # $ % & ' ( ) * + , \ -. / : ; < = > ? @ [ ] ^ _ ` { | }





[[:word:]]





: [--a-zA-Z0-9_]





 





, .., :





[0-9][0-9]\.[0-9][0-9]\.[0-9][0-9][0-9][0-9]





↓





\d\d\.\d\d.\d\d\d\d





, =))





 





:





[ ] \ / ^ $ . | ? * + ( ) { }





, , , - . ( « »).





, \ ( ).





Regex: 2\^2 = 4





: 2^2 = 4





, \Q \E ( ).





Regex: \Q{ ?}\E





: { ?}





 





 





( )

. , email-. :





  • test@mail.ru





  • olga31@gmail.com





  • pupsik_99@yandex.ru





? , , . email — «@» «.».





:





  • / / _





  • @





  • / / _













, «\w», (test), (olga31), (pupsik_99). —  , . — 2 , 2 , 4 . 2, 22 .





—  , .





«+» « », , ! : \w+@





\w, . : \w+@\w+\.





, \w. , . , email :





Regex: \w+@\w+\.\w+





:





test@mail.ru





olga31@gmail.com





pupsik_99_and_slonik_33_and_mikky_87_and_kotik_28@yandex.megatron





, «+»?













?









*









+









 





* — , , «.*» — .





Regex: .*\d\d\.\d\d\.\d\d\d\d.*





:





01.01.2000





09.08.2015! !





! «.*» , :





Regex: .*@.*\..*





:





test@mail.ru





olga31@gmail.com





pupsik_99@yandex.ru





:





@yandex.ru





test@.ru





test@mail.





\w, .





-, — log, log1, log2… log133, * :





Regex: log\d*\.txt





:





log.txt





log1.txt





log2.txt





log3.txt





log33.txt





log133.txt





( ) — , , :





Regex: ?





:













, :





Regex: ()*()*





:

























( — , )





, .





, ? , . « » — \d\d\.\d\d\.\d\d\d\d.





2-4 , 10? ? 10 ? . * :





Regex: \d*\.\d*\.\d*





:





.0.1999





05.08.20155555555555555





03444.025555.200077777777777777





, :













{n}





n





{m,n}





m n





{m,}





m





{,n}





n





 





, \d n , :





\d\d\.\d\d\.\d\d\d\d





\d{2}\.\d{2}.\d{4}





. —  , .





— !





Regex: test{2}





: testt





: testtest





, :





Regex: (test){2}





: testtest





: testt





, , , :





Regex: x\{3\}





: x{3}





, .





Regex: <.*>





:





<req>
<query></query>
<gender>FEMALE</gender>
      
      



:





<req> <query></query> <gender>FEMALE</gender></req>
      
      



HTML XML , , .





, . — . .





, , , :





  1. ,





  2. (, . lazy) — , .





? :





<[^>]*>





, , , «>», . . , — , , .













*





*?





+





+?





{n,}





{n,}?





 





, . =)





 





« ».





Regex:





:

























, . .





, , :





Regex: â„–\d+ \d\d:\d\d





: â„–555 15:30





: â„–555 15:30





:





Regex: .* .*





: ...





: ...





, ? : « .», «...:».





, \b, . , :





Regex: \b\b





:









:





















— Â« , - »:





Regex: \b





:













:

















—  Â« , - »:





Regex: \b





:





















:









\B, - :





Regex: \B\B





:









:













, , :





^ — ()





$ — ()





, , :





Regex: ^ !$





:





!





:





! !





! !





, :













\b









\B









^





()





$





()





 





, —  «»: «! ». , .





. ? . - , . , .





Regex: [ ]+(\w+)[]+\1





: ! . .





, :





[ ]+ → , . , \b.





(\w+) → , . «+» , . , , , . , , . , . , —  1.





[ ]+ → .





\1 → 1. . JavaScript-.





: .













JavaScript





vi





\





Perl





$





PHP





$matches[1]





Java





Python





group[1]





C#





match.Groups[1]





Visual Basic .NET





match.Groups(1)





 





? , HTML, ? , ?





, :





<h2> 2- </h2>
<h3> 3- </h3>
      
      



:





<h2> 2- </h3>
      
      



 





- , . «» .





















(?=)









(?=11)





1





11





113





511





(?!)





( )





(?!11)





1





11





113





511





(?<=)









(?<= )













(?)





( )





( )













 





—  , ! —  :





RegEx:





:





: , !





: , !





, ? , . . ? . .





— . . — , . 1.





RegEx: () \+





: $1





: +





:





« + » ( , , , ). — , , «».





:





RegEx: () \+





: $1





: , + !





: , !





, :





RegEx: () \+ ()





: $2 - $1





: +





: —





—  «, - !», ( ). «».





, , . :





RegEx: ^(, ).*(!)$





: $1$2





( ):





, !





, 777!





:





, !





, .





^ —  .





. — , . 1. , . «, »





«.*» — . . , .





. — , , . ? — «!».





$ —  .





, .





$1 — 1. «, ».





— . , 1, «$1», «,».





$2 — 2, «!»





!





, ? .., --.





— «\d{2}\.\d{2}\.\d{4}». , . :





..





↓





--





, . : (\d{2})\.(\d{2})\.(\d{4})





—  . : $3





, : $3-





. , «$2». : $3-$2





, : $3-$2-





, , . , $1. : $3-$2-$1





!





RegEx: (\d{2})\.(\d{2})\.(\d{4})





: $3-$2-$1





:





05.08.2015





01.01.1999





03.02.2000





:





2015-08-05





1999-01-01





2000-02-03





—  , 12 . «done», ! « ?», . .





:





  1. — 





  2. —  « », ,





  3. ( )





10-25. . ? ! , .





, , . , —  .





:





RegEx: \d+\. (.*)





: $1





:





1.





2.





:













. 5 . — !





=)





 





 









10 . — ! , , , . 100 , .





 









— https://ru.wikipedia.org/wiki/_. , . . , , , . , .





— https://tproger.ru/articles/regexp-for-beginners/





 





— . , :





  1. .





  2. Grep- — , .





  3. , — ? - ?





  4. , .





  5. —  ?





  6. .





  7. ...





, , . . .





: « . ». , . .





, , ! , rm ( linux). , , , .





, . - . . , —  ? =)





PD: busque más artículos útiles en mi blog con la etiqueta "útil" . Y hay videos útiles en mi canal de youtube.








All Articles