This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathregex-resum.php
More file actions
207 lines (124 loc) · 3.64 KB
/
regex-resum.php
File metadata and controls
207 lines (124 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/abc/ : abc
/2:3/ : 12:3:4;d;l
\d : 0 to 9
\w : a to z , A to Z , 0 to 9
\s : while space , tab etc..
(\W,\S,\D) : will search for the negation expl W search for anything except a word caracter)
^ : the start of the string (regx)
$ : the end " " " " " " " "
* : repeated zero or more times
+ : repeated one or more times
? : repeated zero or one juste the letter before it !! so you need to use groupe () for a lot of chars
. : single character , letter , number , whitspace etc..
\. : escape it means : .
\$ : $
\* ! *
MetaCharacters (Need to be escaped):
.[{()\^$|?*+
etc...
| : or
patterns are case sensitive means
/abc/ == /Abc/ it is wrong
but we can add ' i ' to make it true
/abc/i == /Abc/
{} : repetetion
d{1} : one d
d{2} : two d at a row
d{1,2} : one or 2 d in a row
d{1,} : one or more d at a row
[] : match one of any char in the brakckets and only one
[ - ] : add a range
[^ ] : to negate ( negation ) what is inside the brakckets
preg_match($reg_exp,$string) : 1 if it matches or 0 of it doesnt
preg_match("/[a-z]+/", "abcd") -> 1
preg_match($reg_exp,$string,$matches)
preg_match("/[a-z]+/", "abcd",$matches)
$matches => ["abcd"]
regex dont allow us to capture text so we use parentheses to capture as a groupe
()
preg_match($reg_exp,$string,$matches)
$reg_exp : /a([123]+)b/
$String : a222b
$matches : [ 0 => "a222b"
1 => "222"
]
$reg_exp : /([a-zA-Z]+) (\d+)/
$String : Jan 1998
$matches : [ 0 => "Jan 1998"
1 => "Jan"
2 => "1998"
]
(?<name>regex) = give the groupe a name
$reg_exp : /(?<month>[a-zA-Z]+) (?<year>\d+)/
$String : Jan 1998
$matches : [ ...,
"month" => "Jan"
"year" => "1998"
]
preg_replace($reg_exp,$replace,$string)
$reg_exp : /\d+/
$replace : --
$string : abcd123456efg
=> abcd--def
$reg_exp : /\s+/
$replace : ,
$string : a b c d
=> a,b,c,d
to refer to a capture groupe we use \1 for the first one \2 \3 etc...
$reg_exp : /(\w+) and (\w+)/
$replace : \1 or \2
$string : Amine and Rym
=> Amine or Rym
exemples :
/ab\d/ : ab2316545649843
/ \d\d / : ab23
/ \w\s\d / : ab 34
/^abc/ : abcds , abc564 ,abcazer899
/^abc$/ : abc , abclskfabc
/a*bc/ : bc
/a+bc/ : abc , aaaabc
/ab.de/ : abcde , ab de , ab4de , ab-de
/abc.*/ : (any number of any charatcers) abcdef , abc
/abc\./ : abc.
/abc/i : Abc , abc
/a[123]c/ : a1c , a2c , a3c
/a[123]+c/ : a12321231c
/a[1-4]c/ : a4c , a2c
/[a-z0-9 ]+/ : hello there , im 18 years old ( whitspace numbers letters )
/a[^123]b/ : all the caracters except 1,2,3 : a4b , a9b ,a7b
/[^a-z]+/ : HELLO WORLD
String = "^My name is amine"
preg_match_all("/\^M.*e$/",$string,$array);
'debut vc ^M puis tout les mots apres puis fini vc e '
print_r($array);
Mr. Amine
Mr Smith
Mrs. Robanson
Mr T
M(r|s|rs)\.?\s[A-Z]\w* "start with M and then a groupe of r or s or rs and then the char . that it is optional and a space and then a start of a uppercase name and then a bunch of letters that are also optional "
CoreyMSchafer@gmail.com
corey.schafer@university.edu
corey-321-schafer@my-work.net
this case :
[a-zA-Z0-9.-]+@[a-zA-Z-]+\.(com|edu|net)
emails in general :
[a-zA-Z0-9._+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
https://www.google.com
http://coremys.com
https://youtube.com
https://www.nasa.gov
:
https?://(www\.)?\w+\.\w+
ctrl + h : put the regex with groups https?://(www\.)?(\w+)(\.\w+)
and then replace with what you want using backreferencing \1 \2 etc.. :
groupe 1: \1
groupe 2: \2
( its like preg_replace )
also like this :
\1\2
?>