0

In java, how to write a regex to take first 3 lines if there is more than 3 lines OR take all lines if there is less than or equal to 3 lines?

I used the https://regexr.com/ to verify my own solution, failed. Then I asked the online chat group IRC #regex, the expert gives me answer within 1 minute. So I believe I shall share the knowledge here.

user84592
  • 4,750
  • 11
  • 55
  • 91

1 Answers1

0
[^\n]*\n?[^\n]*\n?[^\n]*\n?

One could verify it at website: https://regexr.com/

user84592
  • 4,750
  • 11
  • 55
  • 91