双破正则表达式分割线 [英] Regular Expression Splitting line on double break

查看:55
本文介绍了双破正则表达式分割线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建这个小应用程序,该应用程序从文本文件中提取文本并从文本中创建图像.文本文件包含选择题为多或真/假选项的问题,并且有两个双行换行符来表示新问题的开始.因此,我的测试文件中包含三个问题,如下所示:

I''m trying to create this little app that takes text from a textfile and creates images out of the text. The textfile contains questions with multiple choice or true/false options for answers and there is a double line break to signify the start of a new question. So my test file with three questions in it looks like this:

Testing creating a question. Do you know the answer?<br />
A) first answer<br />
B) second answer<br />
C) doodles<br />
<br />
Testing creating a second question.<br />
T) true<br />
F) False<br />
<br />
Third question will go right here?<br />
A) green<br />
B) blue<br />
C) red<br />
D) apples<br />
E) all of the above



我遇到的麻烦是,稍后我只想获取第一个问题及其答案的内容.我想使用拆分,但无法弄清楚如何在双换行符上拆分.我最后的努力是使用正则表达式.



The trouble I''m having is that later I want to grab just the text for the first question and it''s answers. I wanted to use a split but can''t figure out how to split on a double line break. My last effort was using regular expressions.

Dim strFullText As String = System.IO.File.ReadAllText(txtTextDoc.Text)
Dim myRegex As New System.Text.RegularExpressions.Regex("[\n\n]")
For Each strQuestion As String In myRegex.Split(strFullText)
    '...code to process the question goes here

Next



我已经尝试过[\n\n]的各种变体,但是它总是只返回文本的第一行或文件中的所有文本.

我觉得应该有一种非常简单的方法可以通过某种拆分来完成此任务.任何人有任何想法吗?



I''ve tried all kinds of variations of [\n\n] but it always returns either just the first line of text or all of the text in the file.

I feel like there should be a really easy way to accomplish this with some kind of split. Anyone have any ideas?

推荐答案

两件事:
1.尝试使用"\ r \ n \ r \ n"(大多数Windows应用程序将\ r \ n用作换行符,而不仅仅是\ n)
2. [\ n \ n]正在创建仅包含\ n的字符类.在方括号中重复字符无效.
Two things:
1. Try using "\r\n\r\n" (most windows applications use \r\n as a line break, not just \n)
2. [\n\n] is creating a character class with just \n in it. Repeating characters in square brackets has no effect.


Dim myRegex As New System.Text.RegularExpressions.Regex("\r\n\r\n|\n\n")


在我看来您需要正则表达式为"[\ n] [\ n]".

史蒂夫
Seems to me that you would need "[\n][\n]" as your regular expression.

Steve


这篇关于双破正则表达式分割线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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