英文:
regexp for matching words after many spaces
问题
I'm here to help with the translation. The part you want to extract from the provided text is the numbers before "ms," which are: 1.66, 3.08, 2.67, and 1.36.
In Chinese:
我在这里帮助你进行翻译。您想从提供的文本中提取的部分是在"ms"之前的数字,它们分别是:1.66、3.08、2.67和1.36。
英文:
I have such text:
ConvolutionDepthWise Conv_3 1.66ms | [ 32, 32, 6 *4] -> [ 16, 16, 6 *4] kernel: 3 x 3 stride: 2 x 2
Convolution Conv_4 3.08ms | [ 16, 16, 6 *4] -> [ 16, 16, 6 *4] kernel: 1 x 1 stride: 1 x 1
Convolution Conv_6 2.67ms | [ 32, 32, 6 *4] -> [ 32, 32, 6 *4] kernel: 1 x 1 stride: 1 x 1
ConvolutionDepthWise Conv_8 1.36ms | [ 32, 32, 6 *4] -> [ 16, 16, 6 *4] kernel: 3 x 3 stride: 2 x 2
I'm trying to match substr before ms
, i.e. 1.66
3.08
2.67
1.36
I tried such regexp:
re.findall(r"\s*(.*?)ms", text)
But it always match texts started with the first space.
答案1
得分: 1
A positive lookahead would help
\d+\.\d+(?=ms)
解释:
\d+
: 匹配一个或多个数字 (+
)\.
: 匹配一个.
\d+
: 匹配小数部分的一个或多个数字(?=ms)
: 正向预查匹配模式之前的ms
re.findall(r"\d+\.\d+(?=ms)", text)
# ['1.66', '3.08', '2.67', '1.36']
英文:
A positive lookahead would help
\d+\.\d+(?=ms)
Demo: regex101
Explaination:
\d+
: match one or many digits (+
)\.
: match a.
\d+
: match one or many digits for fractional part(?=ms)
: positive lookahead match pattern aheadms
re.findall(r"\d+\.\d+(?=ms)", text)
# ['1.66', '3.08', '2.67', '1.36']
答案2
得分: 0
使用 re.findall
应该在这里起作用:
inp = """ConvolutionDepthWise Conv_3 1.66ms | [ 32, 32, 6 *4] -> [ 16, 16, 6 *4] kernel: 3 x 3 stride: 2 x 2
Convolution Conv_4 3.08ms | [ 16, 16, 6 *4] -> [ 16, 16, 6 *4] kernel: 1 x 1 stride: 1 x 1
Convolution Conv_6 2.67ms | [ 32, 32, 6 *4] -> [ 32, 32, 6 *4] kernel: 1 x 1 stride: 1 x 1
ConvolutionDepthWise Conv_8 1.36ms | [ 32, 32, 6 *4] -> [ 16, 16, 6 *4] kernel: 3 x 3 stride: 2 x 2"""
matches = re.findall(r'\b(\d+(?:\.\d+)?)ms\b', inp)
print(matches) # ['1.66', '3.08', '2.67', '1.36']
希望这个翻译对您有帮助。
英文:
Using re.findall
should work here:
<!-- language: python -->
inp = """ConvolutionDepthWise Conv_3 1.66ms | [ 32, 32, 6 *4] -> [ 16, 16, 6 *4] kernel: 3 x 3 stride: 2 x 2
Convolution Conv_4 3.08ms | [ 16, 16, 6 *4] -> [ 16, 16, 6 *4] kernel: 1 x 1 stride: 1 x 1
Convolution Conv_6 2.67ms | [ 32, 32, 6 *4] -> [ 32, 32, 6 *4] kernel: 1 x 1 stride: 1 x 1
ConvolutionDepthWise Conv_8 1.36ms | [ 32, 32, 6 *4] -> [ 16, 16, 6 *4] kernel: 3 x 3 stride: 2 x 2"""
matches = re.findall(r'\b(\d+(?:\.\d+)?)ms\b', inp)
print(matches) # ['1.66', '3.08', '2.67', '1.36']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论