正则表达式匹配变量多行? [英] Regular Expression Match variable multiple lines?

查看:111
本文介绍了正则表达式匹配变量多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下文本,我想提取数字开头和数字结尾之间的文本,其中包含动态的行数,并且唯一会改变其中数字的内容例如:首先,第二个,依此类推。我将要从中提取数据的每个文件在数字开头和数字结尾之间具有不同数量的行。我如何编写一个正则表达式来匹配数字开头和数字结尾之间的内容,而又不知道文件在数字开头和数字结尾之间会有多少行?

Lets say I have the following text and I want to extract the text between "Start of numbers" and "End of numbers" there are dynamic amount of lines and the only thing which changes in the numbers in them eg: first, second, etc. Each file I'll be extracting data from has different amount of lines between between "Start of numbers" and "End of numbers". How can I write a regex to match the content between "Start of numbers" and "End of numbers" without knowing how many lines will be in the file between Start of numbers" and "End of numbers"?

致谢!

This is the first line This is the second line

Start of numbers

This is the first line
This is the second line
This is the third line
This is the ...... line
This is the ninth line

End of numbers


推荐答案

您应该使用 SingleLine 模式,该模式告诉C#正则表达式匹配任何字符(不是 \n 以外的任何字符)。

You should use the SingleLine mode which tells your C# regular expression that . matches any character (not any character except \n).

var regex = new Regex("Start of numbers(.*)End of numbers",
                  RegexOptions.IgnoreCase | RegexOptions.Singleline);

这篇关于正则表达式匹配变量多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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