- Practical Network Automation
- Abhishek Ratan
- 276字
- 2021-07-02 14:53:08
Using regular expressions (regex)
There are times when an engineer wants to parse specific data from a sentence or a big chunk of data. Regex is the best tool of the trade for this purpose. Regex is a common concept in every programming language, with the only difference being the syntax in each programming language.
The following example shows how to use regex in Python:
import re
sample="From Jan 2018 till Nov 2018 I was learning python daily at 10:00 PM"
# '\W+' represents Non-Alphanumeric characters or group of characters
print(re.split('\W+', sample))
#Extract only the month and Year from the string and print it
regex=re.compile('(?P<month>\w{3})\s+(?P<year>[0-9]{4})')
for m in regex.finditer(sample):
value=m.groupdict()
print ("Month: "+value['month']+" , "+"Year: "+value['year'])
# to extract the time with AM or PM addition
regex=re.compile('\d+:\d+\s[AP]M')
m=re.findall(regex,sample)
print (m)
The sample output is as follows:
>
['From', 'Jan', '2018', 'till', 'Nov', '2018', 'I', 'was', 'learning', 'python', 'daily', 'at', '10', '00', 'PM']
Month: Jan , Year: 2018
Month: Nov , Year: 2018
['10:00 PM']
As we can see in the preceding output, the first line of code, is a simple sentence split into separate words. The other output is a regex in a loop, which extracts all the months and years depicted by three characters (mmm) and four digits (yyyy). Finally, in the last line of code, a time extraction (extracting a time value using regex) is performed, based upon AM/PM in the hh:mm format.
- Instant Raspberry Pi Gaming
- 基于LabWindows/CVI的虛擬儀器設計與應用
- 空間機器人遙操作系統及控制
- 水晶石精粹:3ds max & ZBrush三維數字靜幀藝術
- 大數據技術與應用
- INSTANT Munin Plugin Starter
- 基于RPA技術財務機器人的應用與研究
- Building Google Cloud Platform Solutions
- Xilinx FPGA高級設計及應用
- 貫通Java Web輕量級應用開發
- Eclipse RCP應用系統開發方法與實戰
- ARM體系結構與編程
- Flink內核原理與實現
- 中小型網站建設與管理
- 大話數據科學:大數據與機器學習實戰(基于R語言)