Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

VB
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


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?
Posted
Comments
Sergey Alexandrovich Kryukov 3-Nov-11 0:29am    
Gosh, why, why doing all that?
--SA
Kschuler 3-Nov-11 9:11am    
Do you see an easier way?

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.
 
Share this answer
 
Comments
Kschuler 3-Nov-11 9:19am    
Yipes. I really should have thought of the cr and lf being different. It has been a while since I've worked with regular expressions. Thanks for your help!
VB.NET
Dim myRegex As New System.Text.RegularExpressions.Regex("\r\n\r\n|\n\n")
 
Share this answer
 
Comments
Kschuler 3-Nov-11 9:20am    
Worked like a charm. Thanks for you help!
Seems to me that you would need "[\n][\n]" as your regular expression.

Steve
 
Share this answer
 
Comments
Kschuler 3-Nov-11 9:17am    
Using this as the regular expression still brought back the entire text from the text file. AspDotNetDev's solution included \r which is the carriage return. In my test data I must have a carriage return then a line feed, which I really should have thought of. Thanks for your help, though!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900