简体   繁体   English

使用正则表达式捕获两个单词之间的文本

[英]Capturing text between two words using a regular expression

I am trying to get the text in between two keywords using a regular expression in CSharp. 我试图在CSharp中使用正则表达式在两个关键字之间获取文本。 Although I already found a topic with the same heading, that one was about finding the text in between square brackets, which is rather easy, since you can use \\[(?<blah>[^\\]]+?)\\] to do this. 虽然我已经找到了一个带有相同标题的主题,但那个是关于在方括号之间找到文本,这很容易,因为你可以使用\\[(?<blah>[^\\]]+?)\\]来做这个。

What I am trying to do is finding the words Matched text 123./! 我想要做的是找到Matched text 123./! in UnMatched text 123./!team . UnMatched text 123./!team So my delimiters are Un and team . 所以我的分隔符是Unteam If I would build my RegEx the way I am used to, I would need to do three parts again: Un for the start delimiter at the beginning, team for the end delimiter at the end and a group (?<blah>...+?) which says "Anything but the string team ". 如果我按照我习惯的方式构建我的RegEx,我需要再做三个部分: Un用于开头的开始分隔符, team用于结束分隔符和一个组(?<blah>...+?)其中写着“除了弦team任何东西”。 But I dunno how to express this in regular expressions. 但我不知道如何在正则表达式中表达这一点

Is there a way to say "not this string" instead of "not one of those characters"? 有没有办法说“不是这个字符串”而不是“不是其中一个字符”? Also since I don't know about differences between implementations of Regular Expressions: I am using System.Text.RegularExpressions.RegEx of the .NET-Framework to parse them, so of course the sample should be working with this implementations. 此外,因为我不知道正则表达式的实现之间的差异:我使用.NET-Framework的System.Text.RegularExpressions.RegEx来解析它们,所以当然样本应该使用这个实现。

You may use this syntax 您可以使用此语法

(?s)(?<=start_delim).+?(?=end_delim)

just replace start_delim and end_delim as required. 只需要根据需要替换start_delimend_delim Visit here for more information in this regard. 有关这方面的更多信息,请访问此处

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

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