正则表达式隔离特定子字符串(Regex to isolate a specific substring)

编程入门 行业动态 更新时间:2024-10-26 06:38:07
正则表达式隔离特定子字符串(Regex to isolate a specific substring)

我有一个我从File.ReadAllText检索的字符串:

6 11 rows processed

如您所见,总是有一个整数指定此文档中的行号。 我感兴趣的是它之后的整数和“行处理”字样。 所以在这种情况下,我只对子串“11行处理”感兴趣。

因此,知道每一行将以整数开始,然后是一些空格,我需要能够隔离它后面的整数和“行处理过的”字样并将其自身返回给字符串。

有人告诉我这很容易用Regex,但到目前为止我还没有最微弱的线索如何构建它。

I have this string I have retrieved from a File.ReadAllText:

6 11 rows processed

As you can see there is always an integer specifying the line number in this document. What I am interested in is the integer that comes after it and the words "rows processed". So in this case I am only interested in the substring "11 rows processed".

So, knowing that each line will start with an integer and then some white space, I need to be able to isolate the integer that follows it and the words "rows processed" and return that to a string by itself.

I have been told this is easy to do with Regex, but so far I haven't the faintest clue how to build it.

最满意答案

这应该适合您的需要:

\d+(.*)

这将搜索一个或多个数字( \d+ ),然后将所有内容放入一个组中:

. = any character * = repeater (zero or more of the preceding value (which is any character in the above () = grouping

但是,杰森是正确的,因为你只需要使用分割功能

This should work for what you need:

\d+(.*)

This searches for 1 or more digits (\d+) and then it puts everything afterwards in a group:

. = any character * = repeater (zero or more of the preceding value (which is any character in the above () = grouping

However, Jason is correct in that you only need to use a split function

更多推荐

本文发布于:2023-07-22 12:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定子   字符串   正则表达式   Regex   specific

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!