首页 > 解决方案 > Regex to catch comma-separated decimal numbers not inside latex command

问题描述

I am trying to create a regex to catch comma-separated decimal numbers not inside LaTeX command \num{}.

Here are examples of what I want to catch:

2,47
12,67
\mathbf{12,76}

Here are examples that I don't want to catch:

\num{2,76}
\num{12,76}

I tried this regex but it doesn't work.

(?<!num\{)(\d*\,\d*)

Thanks!

标签: regex

解决方案


You could prepend the pattern with a word boundary and if you want to exclude multiple patterns, you could add another lookbehind.

Note that you don't have to escapte the { and the , and using \d* matches 0 or more digits.

\b(?<!num{)(?<!SI{)\d+,\d+

See a regex demo


推荐阅读