Quantify new line (\ n) in regex

-1

Is there any way to quantify many paragraphs without being literally to use with PHP ? \d{54} , for example it represents 54 digits, but you can not use \n{54} .

I want one regex to get a data that is 54 new lines from the rest.
Typing \n 54 times is an impractical alternative, I believe.

    
asked by anonymous 04.10.2017 / 03:55

2 answers

1

I do not know if I understand. Would you like to take the first 54 lines of a text? Is this what you are trying to do?

(.*\n){54}

If it is not, of more details showing what you have on the screen, and what you want to get. It would be easier to understand by visualizing what you have and what you want.

EDITED

I do not know if you can use naming the catch, but if you can, I think that would make it easier:

/(?P<paragrafo>.*)(.*\n){1,54}/g

Or so it should work:

/(.*)(.*\n){1,54}/g

The first capture (.*) will take the first paragraph, the second capture (.*\n){1,54} will take 54 paragraphs counting also the first one. But then in your code just ignore this second capture. And the modifier at the end /g will ensure that the regex will take captures as long as you find text.

The schematic that I put {1,54} is that it will execute until the end, even if the last capture battery does not have 54 complete paragraphs. If you put it just like this: {54} It will require that all capture batteries have 54 paragraphs, and if the last one has for example 53 paragraphs or less, it will ignore and will not execute the first capture of the last battery.

I hope you have not been so confused to understand ^^

    
04.10.2017 / 05:29
1
  

Is there any way to quantify many paragraphs without being   literally to use with PHP?

Yes, you can use the .* ken token in conjunction with \n to capture an entire paragraph. Then put them in a catch group and quantify the group: (.*\n){999}

  

I want in a single regex to get a data that is 54 new lines of the rest.

You can use:

(.*\n){54}(.*)

Explanation

  • (.*\n){54} Captures all irrelevant data from the 54 lines
  • (.*) After capturing the 54 irrelevant lines, that part picks up the data until the next line break, that is, it captures everything in paragraph 55.
  • Capture group 1 - will capture all irrelevant data (only in a capture group so the token sequence can be quantified).
  • Capture group 2 - will capture the data you want, everything on line 55.

You can see the operation of this regex here.

    
04.10.2017 / 15:04