I have a huge string and I am trying to parse it. What I want to do is read the clk
(clock) values. Is there any way to extract this text(the text between the {[ ]}, in a Pythonic way?
The goal is to take the clk
values and store them in a list.
Assuming that you can't have bracketed values inside other bracketed values, it's easy enough to capture each bracketed value with regex:
import re
s = """1. e4 {[%clk 1:23:29]} c5 {[%clk 1:30:39]} 2. Nf3 {[%clk 1:23:32]} d6
{[%clk 1:30:58]} 3. d4 {[%clk 1:23:50]} cxd4 {[%clk 1:31:21]}"""
results = re.findall(r"{\[%clk (.*?)\]}", s)
print(results)
Result:
['1:23:29', '1:30:39', '1:23:32', '1:30:58', '1:23:50', '1:31:21']
Quick explanation of the regex pattern:
{ #literal curly bracket
\[ #literal square bracket
%clk #literal "%clk "
(.*?) #match any number of characters, and capture them for the output
\] #literal square bracket
} #literal curly bracket
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.