匹配多行直到sepertor行? [英] Matching multiple lines up until a sepertor line?

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

问题描述

学习自己一些Regex,同时试图解析数据表,我认为没有一个简单的方法(在Regex,我的意思是在C#,肯定!)做到这一点。假设我有一个包含行的文件:

Learning myself some Regex, while trying to parse a datasheet, and I'm thinking there's not an easy way (in Regex, I mean.. in C#, sure!) to do this. Say I have a file with the lines:

0000AA  One Token    -  Value
0000AA  Another Token-  Another Value
0000AA  YA Token     -  Yet Another
0000AA  Yes, Another -  Even More
0000AA
0000AA  ______________________________________________________________________
0000AA  This line - while it will match the regex, shouldn't.

所以我有一个简单的多行regex:
\s * [AZ] {2} [0-9] {4} \s\s *(?< token>。*?)\-(?< value& / code>
这将所有的'Tokens'加载到'token'中,并将所有的值加载到'value'组中。很简单!然而,正则表达式ALSO匹配底线,将这行放入令牌,并且它将[...]转换为值。

So I have an easy multi-line regex: ^\s*[A-Z]{2}[0-9]{4}\s\s*(?<token>.*?)\-(?<value>.*?)$ This loads All the 'Tokens' into 'token', and all the values into 'value' group. Pretty simple! However, the Regex ALSO matches the bottom line, putting 'This line' into the token, and 'while it will [...]' into the value.

基本上,我希望正则表达式只匹配 ____ 分隔符行之上的行。这将是可能的Regex单独,或者我需要修改我的传入字符串的 ____ 分隔符行吗?

Essentially, I'd like the regex to only match the lines above the ____ separator line. Would this be possible with Regex alone, or will I need to modify my incoming string first to .Split() on the ____ separator line?

全部欢呼 -
- 同。

Cheers all -
-Mike.

推荐答案


'd喜欢正则表达式只匹配 ____ 分隔符行上面的行。这是否可能与正则表达式单独?

I'd like the regex to only match the lines above the ____ separator line. Would this be possible with Regex alone?

当然可以。添加前瞻以确保这样的行跟随,例如:

Sure it's possible. Add a lookahead to make sure such a line follows, something like:

(?=(?s).*^\w{6}[ \t]+_{4,})

的表达式,以确保这样的行跟随。例如:

Add this to the end of your expression to make sure that such a line follows. Eg:

(?m)^\s*[A-Z]{2}[0-9]{4}\s\s*(?<token>.*?)\-(?<value>.*)$(?=(?s).*^\w{6}[ \t]+_{4,})

(也添加 m s 表达式中的标志。)

(Also added m and s flags in the expression.)

这不是很有效的正如正则表达式引擎可能需要扫描每个匹配的大部分字符串。

This is not very efficient tho, as the regex engine will probably need to scan through most of the string for every match.

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

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