22.585698    3601.767090     D:\measurements\LabCourse_2015\6\90deg\image0001_23_mT.png
20.051883    3580.268555     D:\measurements\LabCourse_2015\6\90deg\image0002_20_mT.png
17.587862    3550.807617     D:\measurements\LabCourse_2015\6\90deg\image0003_18_mT.png

This code seems to apply to lines where are two digits. However, I am not sure if it is well written otherwise. Difficult to read.

How can you improve this regex?


Base on your output, I offer some points to improve your regex:

  • You can match explicit at beginning of line, using ^
  • If you don't use the match groups, then you don't need parentheses or using non-capturing group ?:
  • \s includes \t, so you only need \s.

The final regex can be m/^(?:[+-]?\d+\.\d+)\s+(?:[+-]?\d+\.\d+)/


it matches lines that begin with two optionally signed numbers having decimal parts, with said numbers separted by ascii whitespace. it seems optimal and clear as it stands.


An optimization I would add to cuonglm's suggestions, if only to make the regex easier to maintain:


This way, you can concentrate any further optimizations to the regex in one place.

Also, if you know that the numbers are proper decimal numbers (i.e. your data doesn't contain more than one decimal dot in a string of digits):


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.