Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include a multiline text in regex pattern

I have a pattern ^[0-9]+$, but I want it to include a \n new-line symbol so that the string like below would be valid:
123\n345\n678\n9752\n or in other words:

123
345
678
9752

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Assuming you don’t want to include leading/trailing newlines, try:

\A[0-9]+(?:\n[0-9]+)*\Z

See an online demo.


  • \A – Start-string anchor;
  • [0-9]+ – 1+ digits;
  • (?:\n[0-9]+)* – Match nested non-capture group 0+ times validating a single newline character and 1+ digits;
  • \Z – End-string anchor.

Note: As per my comments, ^[0-9]+(?:\n[0-9]+)*$ would also work with the right flags turned on/off.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading