Expressões regulares (também chamadas de regexp ou regex ) são um mecanismo para localizar e substituir texto. Em uma linha, em um arquivo, em vários arquivos ... Desenvolvedores os utilizam no código da aplicação, testadores em autotestes ou apenas quando trabalham na linha de comando!
Por que Ă© melhor do que uma simples pesquisa? Aquele que permite definir o modelo.
Por exemplo, a data de nascimento vem para a entrada no formato DD.MM.AAAAA. Você precisa transferi-lo posteriormente, mas no formato AAAA-MM-DD. Como posso fazer isso com uma pesquisa simples? Você não sabe com antecedência qual será a data.
« - ».
?
, test ( )
grep-
...
— , . , . 200, . ( Notepad++ ).
, . .
. , — , , , . :
,
. — , .
: , ,
Regex:
: , ,
, regex, . BACKGROUND-COLOR , ( ) , , regex :
, «», «». !
, , . JavaScript i, . (notepad++) «Match case». , . , , .
, ?
: , , ,
Regex:
: , , ,
. JavaScript g (global), , .
, , ? :
: , 55,
Regex:
: , 55,
. . , , , - . - ... ? . ?
, «». , , :
Regex:
:
, . . , , . , .
. — ().
:
Regex: .
:
, , , . , :
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
Regex: \Q{ ?}\E
: { ?}
( )
. , email-. :
test@mail.ru
olga31@gmail.com
-
? , , . 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 , , .
, . — . .
, , , :
,
(, . 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) |
11 113
|
(?!) |
( ) |
(?!11) |
1
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», ! « ?», . .
:
—
— « », ,
( )
10-25. . ? ! , .
, , . , — .
:
RegEx: \d+\. (.*)
: $1
:
1.
2.
:
. 5 . — !
=)
10 . — ! , , , . 100 , .
— https://ru.wikipedia.org/wiki/_. , . . , , , . , .
— https://tproger.ru/articles/regexp-for-beginners/
— . , :
.
Grep- — , .
, — ? - ?
, .
— ?
.
...
, , . . .
: « . ». , . .
, , ! , rm ( linux). , , , .
PS - Procure artigos mais Ăşteis no meu blog sob a tag “útil” . E vĂdeos Ăşteis estĂŁo no meu canal do youtube