Jump to content

Recommended Posts

Hello everybody,

(Sorry for my english, I'm practicing :) !)

I was trying to figure out how to cut a whole text file with the following pattern :

 

Question #10/10
Question title: (1 answer)
(*) - first
( ) - Second


Question #09/10
Question title: (2 answer)
[*] - first
[ ] - Second
[*] - 3
[ ] - 4


 

So

1st line : Question #X/Y

2nd line : The question...always in one line

3 to ... N lines : possible answers\r\n

\r\n

\r\n

1st line and so on....

 

 

So I open the content of the file :

$file = file_get_contents("file.txt");
$reg='/(?!(\r\n){3}).*(\r\n){3}/esiU';
preg_match_all($reg, utf8_decode($file ),$reg_array);

And I can get$reg_array containing all block of question/answers...

 

where i'm blocking right now(I know I could have do it in one single regexp...), is to separate the number of the question, and...the variable number of lines of possible answers before the 2 empty lines...

 

I don't necessary want the solution, but at least some good hints to continue my tries...

I'm not able to select only the first line (didnt find this part on the internet...), nor the "second", nor the "rest without the empty lines"

 

 

 

in advance,thanks for your patience and your help !

Thepi

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/205703-separate-multiline-blocks/
Share on other sites

$pattern = '/(.+(?:\n){0,1})/'; // Zach's
$pattern = '/(.+(?!\n))/'; // thepi's

 

Both of those are trying to be fancy where they needn't be so. The pattern /.+/ will do what it appears you want (match single lines of text, without newlines).  The outer parentheses are unnecessary since the whole matched string is available with $match[0] (or $0 in a replacement). The check for the existence (or not) of a newline is also redundant since the dot metacharacter will never match one; its greediness will consume the whole line of text stopping at a newline (or the end of the subject string).

$pattern = '/(.+(?:\n){0,1})/'; // Zach's
$pattern = '/(.+(?!\n))/'; // thepi's

 

Both of those are trying to be fancy where they needn't be so. The pattern /.+/ will do what it appears you want (match single lines of text, without newlines).  The outer parentheses are unnecessary since the whole matched string is available with $match[0] (or $0 in a replacement). The check for the existence (or not) of a newline is also redundant since the dot metacharacter will never match one; its greediness will consume the whole line of text stopping at a newline (or the end of the subject string).

 

Hey salathe, thanks for the explanations, would be usefull :)

Basically I tried with "." ("Any character except newline" cheat sheet), but I wanted something more complex... Once here by formulating my thoughts I saw I could do it line by line ! /.+/ would have work fine just for the line by line !

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.