0

I have a text file where all the question and answer is there like bellow text

question l : Regular expression problem in C#  

answer 1 :   regular expression answer 1

question  2 : Regular expression problem in php 

answer 2 :   regular expression answer 2

etc...

I made this like bellow

<question> question l : Regular expression problem in C# </question> 

<answer> answer 1 : regular expression answer 1</answer>

<--page-->

<question>question 2 : Regular expression problem in php </question>

<answer>answer 2 : regular expression answer 1</answer>

<--page-->

So that i can made a array and foreach loop i can insert that to my data base in bellow table

question_id, question answer

1 Regular expression problem in C# regular expression answer 1

2 Regular expression problem in php regular expression answer 2

any one can help me to build the regular expression, i build this one but it include question text in answer text

preg_match_all("#^(?<!<question>).*<question>.*?</question>(?!.*<question>)#",$temp_data,$question);
preg_match_all("#^(?<!<answer>).*<answer>.*?</answer>(?!.*<answer>)#",$temp_data,$answer);

1 Answer 1

1

I am not sure I can completely follow, but I think you wanted this

preg_match_all("#(?<=<question>).*?(?=</question>)#",$temp_data,$question);
preg_match_all("#(?<=<answer>).*?(?=</answer>)#",$temp_data,$answer);

The first one would match anything between a <question> tag and the next </question> tag, but not the tags itself. See it on Regexr

The second would match anything between a <answer> tag and the next </answer> tag, but not the tags itself.

Sign up to request clarification or add additional context in comments.

1 Comment

yes i want to get text all inside <question> tags </question> tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.