简体   繁体   English

\\ begin {enumerate}和\\ end {enumerate}之间的正则表达式

[英]Regex between \begin{enumerate} and \end{enumerate}

So, I have a string like 所以,我有一个像

\begin{enumerate}
\item My first item 
\item My second item
\end{enumerate}

And need with a Regex get the part between \\begin{enumerate} and the \\end{enumerate} . 并且需要使用Regex来获得\\begin{enumerate}\\end{enumerate}之间的部分。 I've tried to have the pattern \\\\begin{enumerate}(.*?)\\\\end{enumerate} and in online checkers it works but not when I run my code. 我尝试使用\\\\begin{enumerate}(.*?)\\\\end{enumerate}并且在在线检查器中它可以工作,但在我运行代码时不起作用。

Anyone in a mind of helping me out I would appreciate it. 任何想帮助我的人都将不胜感激。

You can use [\\s\\S]* instead of .* or use modifier s , since . 您可以使用[\\s\\S]*代替.*也可以使用modifier s ,因为. does not contain \\n 不包含\\n

var re = new Regex(@"\\begin{enumerate}([\s\S]*?)\\end{enumerate}");

You can use this regex: 您可以使用此正则表达式:

(?s)\\begin{enumerate}(.*?)\\end{enumerate}

C#: C#:

var rgx = new Regex(@"(?s)\\begin{enumerate}(.*?)\\end{enumerate}");

(?s) in the beginning of the pattern forces a dot to match newline symbols, and to match a backslash, you need to double it in a verbatim string literal, or quadruple it in a normal regular string literal. 模式开头的(?s)强制点与换行符匹配,并与反斜杠匹配,您需要在逐字字符串文字中将其加倍,或在常规的常规字符串文字中将其加倍。

Perhaps, you'd like to enforce case insensitive matching, too. 也许,您也想强制执行不区分大小写的匹配。 Then, replace (?s) with (?si) . 然后,将(?s)替换为(?si)

Tested in Expresso: 在Expresso中测试:

在此处输入图片说明

I suppose you have an escaping problem here, use verbatim strings. 我想您在这里有一个转义问题,请使用逐字字符串。 Also, the braces should be escaped. 另外,大括号也应脱开。 And finally, did you use RegexOptions.Singleline so that the . 最后,您使用了RegexOptions.Singleline从而使. metacharacter is allowed to match newlines? 元字符可以匹配换行符?

var re = new Regex(@"\\begin\{enumerate\}(.*?)\\end\{enumerate\}", RegexOptions.Singleline);

Note the @ just before the " . 注意@只是之前"

It looks as thought you may only want to use the text in between these tags so if thats the case you could use a positive lookbehind and lookahead and that would get you: 似乎以为您可能只想在这些标签之间使用文本,因此,在这种情况下,您可以使用正向查找和向前查找,这将使您:

\\n\\item My first item \\n\\item My second item\\end \\ n \\ item我的第一项\\ n \\ item我的第二项\\结束

so try this instead. 所以试试这个。 I have added some escape characters for you so it will work. 我为您添加了一些转义符,因此它将起作用。

(?<=\\begin{enumerate}).*?(?=\\end{enumerate}) (?<= \\ {开始枚举})。*?(?= \\ {端枚举})

I would probably use the @ symbol instead of the escape characters as with the second answer. 与第二个答案一样,我可能会使用@符号代替转义字符。 I think it looks easier to read. 我认为它看起来更容易阅读。

You could always do this with plain old Linq. 您总是可以使用简单的旧Linq来执行此操作。 Eg: 例如:

var s = "\\begin{enumerate}\n" +
        "\\item My first item\n" +
        "\\item My second item\n" +
        "\\end{enumerate}";

var allRows = s.Split('\n').ToList();

var items = allRows
        .Skip(1)
        .Take(allRows.Count - 2)
        .Select(i => i.Replace(@"\item ", String.Empty));

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

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