-1

I have a text that has many questions and each question has a seperator QId {QuestionId}.

Now I want to split this text with QId {QuestionId} to get a dictionary with keys as {QuestionId} and the question body as a Value. Here questionId is dynamic will have any Integer value.

I have tried this Regex.Split with regex "\\s*QId\\s*[\\d]+\\s*\\s*". I can get the question body but I want Question Id, too, as a Key so I can do some action based on QuestionId.

enter image description here

I have tried below code:

Regex.Split(
    text, string.Format("\\s*{0}\\s*[\\d]+\\s*\\s*",
    "QID"), RegexOptions.Singleline);

It did not give me data in dictionary.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

-1

Your input does not look like this:

QID 1 
Where, oh where, did the Joel Data go?

QID 2 
What time is it?

QID 3 
In what time zone?

It's more something like this:

<p><B>QID 1</B><BR>Where, oh where, did the Joel Data go?</p>
<p><B>QID 2</B><BR>What time is it?</p>
<p><B>QID 3</B><BR>  In what time zone?</p> 

This means that you will have to take the html tags into consideration and \s (whitespace) will not be enough.


Html and regex don't mix well and you may want to consider using a html library or some other way (maybe string.Split). Please see, RegEx match open tags except XHTML self-contained tags and its legendary answer.

tymtam
  • 31,798
  • 8
  • 86
  • 126