· 7 years ago · Sep 07, 2018, 07:38 AM
1Regex for capture two types of patterns [closed]
2XXXX-yyyyy+123131331@gmail.com
3XXXX+313131313@gmail
4
5/^w+(-w+)?+d+@S+$/
6
7^ # the start of the string
8w+ # one or more ascii alhpa-nums
9( # start group 1
10 - # the literal '-'
11 w+ # one or more ascii alhpa-nums
12)? # end group 1, and make it optional
13+ # the literal '+'
14d+ # one or more digits
15@ # the literal '@'
16S+ # one or more non-space chars
17$ # the end of the string
18
19^(w+)(?:-w+)?+d+@S+$
20
21/p{Lu}+(-p{Ll}+)?+d+@gmail.com/
22
23/([A-Z0-9._%-]+?)(?:-([A-Z0-9._%]+))?+(d+)@gmail.com/i
24
25p = re.compile(r"([A-Z0-9._%+-]+?)(?:-([A-Z0-9._%+]+))?+(d+)@gmail.com", re.IGNORECASE)
26m = p.search("XXXX-yyyy+123131313@gmail.com")
27m.groups()
28# ('XXXX', 'yyyy', '123131313')
29
30( # start capture group 1
31 [A-Z0-9._%+-] # match the characters in the []
32 +? # one or more times, lazy
33) # end capture group 1
34(?: # start non-capturing group
35 - # match the literal character '-'
36 ( # start capture group 2
37 [A-Z0-9._%+] # match the characters in the []
38 + # one or more times
39 ) # end capture group 2
40)? # end non-capturing group, matching 1 or 0 times
41+ # match the literal character '+'
42( # start capture group 3
43 d+ # match digits [0-9] one or more times
44) # end capture group 3
45@gmail.com # match the literal characters '@gmail.com'
46
47X.X_X%X+X-X-y.y_y%y+y+123@gmail.com => ('X.X_X%X+X-X', 'y.y_y%y+y', '123')