forked from AbeTavarez/Python_DevOps_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreating_basics_regex.py
More file actions
27 lines (20 loc) Β· 836 Bytes
/
Copy pathcreating_basics_regex.py
File metadata and controls
27 lines (20 loc) Β· 836 Bytes
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
import re
# Name of country start and ends with A a
print(re.search(r'A.*a', 'Argentina'))
# This will also match the following country
print(re.search(r'A.*a', 'Azerbaijan'))
# We need to make our pattern strictier
# Adding (^ $) makes sure we only get matching that beggins and ends with A a in this case
# Should not match now
print(re.search(r'^A.*a$', 'Azerbaijan'))
# But still work for
print(re.search(r'^A.*a$', 'Australia'))
# * Checks if variable name is valid
# Pattern Desc
# 1- can only start with letters or underscore
# 2- Can contain letters, numbers and underscores till end of pattern
pattern = r'^[a-zA-Z_][a-zA-Z0-9_]*$'
print(re.search(pattern, '_valid_name')) # ok
print(re.search(pattern, 'no spaces allowed')) # nop
print(re.search(pattern, 'my_var1')) # ok
print(re.search(pattern, '1my_var')) # nop