Quantify new line ( n) in regex

Asked

Viewed 85 times

-1

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

I want in one regex take a dice that is 54 new lines from the rest.
Type \n 54 times is an impossible alternative, I believe.

  • Split by n and take the position of the array

  • Does it have to be using pure regex? No alternatives available in languages?

  • Why \n{54} is not an option?

  • @However, with n{54} I did not get results. You have already succeeded in this way?

  • @Antoniooliveira yes: https://jsfiddle.net/Sergio_fiddle/hLm9f1xv/

  • Funny, it didn’t work now. Thank you!!: https://regex101.com/r/7kyfVA/1

Show 1 more comment

2 answers

1


I’m not sure I understand. Would you like to take the first 54 lines of a text? Would that be what you’re trying to do?

(.*\n){54}

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

EDITED

I don’t know if you can use naming the catch, but if you can I think it would be easier:

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

Or else it should work too:

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

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

The scheme I put in {1,54} is that it will run until the end, even q the last capture battery does not get to have 54 full paragraphs. If put just like this: {54} It will require that all catch batteries have 54 paragraphs, and if the last one has for example 53 paragraphs or less, it will ignore and will not perform the first capture of the last battery.

I hope you’re not too confused to understand ^^

  • text to: "(.?) .... 54 paragraphs after ... text b: (.?), (.?), (.?) ... got it?

  • Ahhh I think I understand yes @Antoniooliveira, I edited my answer putting another solution at the end. Take a look now if this way can solve your case. I’m sorry it took me so long to answer, I’ve been out all day today.

1

Is there any way to quantify many paragraphs other than literally to use with PHP?

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

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

You can use:

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

Explanation

  • (.*\n){54} Captures all irrelevant data from 54 lines
  • (.*) After the capture of the 54 irrelevant lines this part takes the data until the next line break, ie, captures everything that is in paragraph 55.
  • Capture group 1 - will capture all irrelevant data (is only in a capture group for that sequence of tokens can be quantified).
  • Capture group 2 - will capture the data you want, everything that is on line 55.

You can see how this regex works here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.