-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPitfall13 regular expression
33 lines (23 loc) · 1.64 KB
/
Pitfall13 regular expression
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
******************************************************************************************************
In regular expressions, the * character means "match the preceding character zero or more times".
******************************************************************************************************
In your pattern, the preceding character is 4, which means the * matches zero occurrences of 4.
To match the entire string up to the last digit, you can modify the pattern to match any sequence of one or more digits (\d+) at the end of the string, like this:
import re
r1 = r'OPLA-1234\d+$'
regex = re.compile(r1)
opla_job='OPLA-123599'
re.findall(regex, opla_job)
Here, we've removed the * from the pattern and replaced it with \d+, which means "match one or more digits".
We've also added the $ character at the end of the pattern to anchor it to the end of the string.
This ensures that we match only the digits at the end of the string and not any other characters that may follow.
r1 = r'OPLA-1234*' vs r1 = r'OPLA-1234\d+$' vs r1 = r'OPLA-12345*'
**************************************************************************************************************************************************** extension
import re
text = "The price is $3.50. It's 20.5 degrees Celsius today! 0.32 .3 3."
text = re.sub("(?<!\d)\.|\.(?!\d)|[',!@#$?-]", '', text)
print(text)
--->
The price is 3.50 Its 20.5 degrees Celsius today 0.32 3 3
# Combining both parts, the pattern (?<!\d)\.|\.(?!\d) matches any dot character that is not immediately preceded or followed by a digit.
This pattern can be used to remove all dots except for the ones between numbers.