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

Finding numbers with decimal comma in a TeX-File using regular expressions

I have a very large TeX-File wherein we set mathematical text. Unfortunately, TeX sets a empty space after each comma, even though numbers (in Germany) are formatted without space after comma.

I am aware that there are packages that solve this problem automatically, but I tried to find such commas with a regular expression before the TeX formatter would do its thing, so I could wrap these commas in braces. For example, I want 1,2 to become 1{,}2. But my regular expression approach does not work as expected:

I used "[0-9],[0-9]" resp. "\d,\d" to look for all places where two digits are separated with a decimal comma. That works fine. But I was wondering why for input "1,2,3" it only finds the match "1,2" but not "2,3".

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

There must be something I don’t quite understand about regular expressions. How would I have to alter the expression to find overlapping instances of the search pattern?

>Solution :

Once a character has been matched, it is not matched again. As in your example "2" is matched, the "cursor" of the regex engine has now passed that "2", and so the next match can only be found in ",3" — and there no match is found.

To resolve that, use a look-ahead assertion for the decimal digits without actually capturing them:

\d,(?=\d)

The matches will be "1," and "2," and now you can make the replacement as you want (adding braces), knowing these matches guarantee there is a digit following.

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