匹配多个空格之后的单词的正则表达式。

huangapple go评论101阅读模式
英文:

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']

regex101

英文:

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 ahead ms
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 = &quot;&quot;&quot;ConvolutionDepthWise     Conv_3                             1.66ms    |     [ 32,  32,   6 *4] -&gt; [ 16,  16,   6 *4]         kernel: 3 x 3     stride: 2 x 2
Convolution              Conv_4                             3.08ms    |     [ 16,  16,   6 *4] -&gt; [ 16,  16,   6 *4]         kernel: 1 x 1     stride: 1 x 1
Convolution              Conv_6                             2.67ms    |     [ 32,  32,   6 *4] -&gt; [ 32,  32,   6 *4]         kernel: 1 x 1     stride: 1 x 1
ConvolutionDepthWise     Conv_8                             1.36ms    |     [ 32,  32,   6 *4] -&gt; [ 16,  16,   6 *4]         kernel: 3 x 3     stride: 2 x 2&quot;&quot;&quot;

matches = re.findall(r&#39;\b(\d+(?:\.\d+)?)ms\b&#39;, inp)
print(matches)  # [&#39;1.66&#39;, &#39;3.08&#39;, &#39;2.67&#39;, &#39;1.36&#39;]

huangapple
  • 本文由 发表于 2023年3月23日 11:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定