使用 RegEx 查找字符串中特定文本后的数字 [英] Find numbers after specific text in a string with RegEx

查看:49
本文介绍了使用 RegEx 查找字符串中特定文本后的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的多行字符串:

I have a multiline string like the following:

2012-15-08 07:04 Bla bla bla blup
2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla
2012-15-08 07:05 Another text that I don't want to search...
2012-15-08 07:06 Another text that I don't want to search...
2012-15-08 07:06 *** Error importing row no. 5: The import of this line failed because bla bla
2012-15-08 07:07 Import has finished bla bla

我想要的是在正则表达式(使用 PowerShell)的帮助下提取所有有错误的行号.所以我需要找到*** Error importing row no."和下面的:"之间的数字,因为这总是会给我行号.

What I want is to extract all row numbers that have errors with the help of RegularExpression (with PowerShell). So I need to find the number between "*** Error importing row no. " and the following ":" as this will always give me the row number.

我查看了其他各种 RegEx 问题,但说实话,答案对我来说就像中文一样.

I looked at various other RegEx question but to be honest the answers are like chinese to me.

尝试在 http://regexr.com/ 的帮助下构建 RegEx,但到目前为止还没有成功,例如使用以下模式:

Tried to built RegEx with help of http://regexr.com/ but haven't been successful so far, for example with the following pattern:

"Error importing row no. "(.?)":"

有什么提示吗?

推荐答案

试试这个表达式:

"Error importing row no\. (\d+):"

演示

这里需要了解量词和转义序列:

Here you need to understand the quantifiers and escaped sequences:

  • . 任意字符;由于您只需要数字,请使用 \d;如果您指的是句点字符,则必须使用反斜杠 (\.) 将其转义
  • ? 零个或一个字符;这不是您想要的,因为您可以在第 10 行出现错误,并且只取1"
  • + 一个或多个;这对我们来说就足够了
  • * 任意字符数;与 .* 一起使用时,您必须小心,因为它会消耗您的整个输入
  • . any character; as you want only numbers, use \d; if you meant the period character you must escape it with a backslash (\.)
  • ? Zero or one character; this isn't what do you want, as you can here an error on line 10 and would take only the "1"
  • + One or many; this will suffice for us
  • * Any character count; you must take care when using this with .* as it can consume your entire input

这篇关于使用 RegEx 查找字符串中特定文本后的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆