Ekspresi reguler (juga disebut regexp , atau regex ) adalah mekanisme untuk menemukan dan mengganti teks. Dalam satu baris, dalam sebuah file, dalam beberapa file ... Pengembang menggunakannya dalam kode aplikasi, penguji dalam tes otomatis, atau hanya saat bekerja di baris perintah!
Mengapa ini lebih baik daripada pencarian sederhana? Salah satu yang memungkinkan Anda mengatur template.
Misalnya, masukan tanggal lahir dalam format DD.MM.YYYYY. Anda perlu mentransfernya lebih lanjut, tetapi dalam format TTTT-BB-HH. Bagaimana saya bisa melakukan ini dengan pencarian sederhana? Anda tidak tahu sebelumnya tanggal berapa.
« - ».
?
, 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 - Cari artikel bermanfaat lainnya di blog saya dengan tag "berguna" . Dan video yang bermanfaat ada di saluran youtube saya