简体   繁体   English

C#(.net)RegEx.Match在换行符之间匹配子字符串-使用换行符作为正向超前限制

[英]C# (.net) RegEx.Match Substring between newlines - using newline as positive lookahead limit

I have been tinkering with RegEx and got some great results and I want to keep using it. 我一直在修改RegEx,并获得了一些不错的结果,我想继续使用它。

Right now I am stuck at finding a string that is set between 2 newlines. 现在,我一直坚持寻找设置在2个换行符之间的字符串。 Here is the sample target text (note this is one of thousands of possible texts): 这是示例目标文本(请注意,这是数千种可能的文本之一):

Substance information in Wikipedia
FORMULA
CH2O
Grafik
Molar mass: 30,03 g/mol

The target is "CH2O". 目标是“ CH2O”。

I tried (?<=FORMULA).*(?=Grafik) with RegexOptions.Singleline and it starts right after FORMULA but goes all the way down and ignores Grafik . 我尝试使用RegexOptions.Singleline (?<=FORMULA).*(?=Grafik) ,它在FORMULA之后立即开始,但一直向下并忽略Grafik

I tried it without singleline but it returns nothing since the . 我试过没有单行,但自以来没有返回任何内容. stops at the \\n . 停在\\n Since I want the newline as a limit, the following has no singleline. 由于我希望换行符为限制,因此以下没有单行。

The closest I have gotten were these: 我得到的最接近的是:

(?<=FORMULA)[\w\W]+(?=Grafik)
(?<=FORMULA)[\w\W]*(?=Grafik)

However, if the Grafik changes, I'd like to track the newline instead of it. 但是,如果Grafik发生变化,我想跟踪换行符而不是它。

(?<=FORMULA)[\\w\\W]*(?=\\n) or (?<=FORMULA)[\\w\\W]*(?=\\r) will still match Grafik for some reason... (?<=FORMULA)[\\w\\W]*(?=\\n)(?<=FORMULA)[\\w\\W]*(?=\\r)由于某些原因仍会匹配Grafik ...

Does anyone know a more optimal way to make the positive lookahead the newline? 有谁知道一种更理想的方式来使换行符成为积极的先行者?

Please don't answer anything unrelated to RegEx. 请不要回答与RegEx无关的任何内容。

Would this work for you 这对你有用吗

(?<=FORMULA\s+)\S+

Matches everything after FORMULA and before a new line 匹配FORMULA之后和换行之前的所有内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM