0

Code

m/([+-]?\d+\.\d+)[\s\t]+([+-]?\d+\.\d+)/

Data

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?

3

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+)/

1

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.

0

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

m/^(?:[-+]?\d+\.\d+\s+){2}/

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):

m/^(?:[-+]?[\d.]+\s+){2}/

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.