-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.py
More file actions
32 lines (25 loc) · 837 Bytes
/
Copy pathregex.py
File metadata and controls
32 lines (25 loc) · 837 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
28
29
30
31
32
#!c:/Users/barraud/AppData/Local/Programs/Python/Python35/python.exe
import re
# check if a string has a pattern
string = "val=123"
if re.search("\w+?=\d+?", string):
print("found")
# use groups to get a value
match = re.search("\w+?=(\d+?)$", string)
if match:
print(match.group(1))
# regex replace
subs = re.sub("\d+?$", "456", string)
print(subs)
# or use lambda in the repl. the arg is the group object
subs = re.sub("(=)(\d+?)$", lambda x: x.group(1) + "eq", string)
print(subs)
# and wherever a lambda works, you can also use a function pointer
# if you're going to run the same pattern multiple times, then compile the pattern and use it
pattern = re.compile("\w+?=(\d+?)$")
if pattern.search(string):
print("found")
# and for replace
pattern = re.compile("\d+?$")
subs = pattern.sub("456", string)
print(subs)