2

我使用这个正则表达式将千位分隔符放在一个字符串中:

while matchstr(mystr, '\(\d\)\(\d\{3}\)\(\D\|\s\|$\)') != ''
    let mystr = substitute(mystr, '\(\d\)\(\d\{3}\)\(\D\|\s\|$\)', '\1.\2\3', 'g')
endwhile

为了

let mystr = '2000000'

上面的代码给出

2.000.000

问题是当有小数分隔符时,它还会在小数分隔符后面的数字的小数部分放置千位分隔符(以下是逗号)。

例如,

let mystr = '2000000,2346'

导致

2.000.000,2.346

虽然我希望它是

2.000.000,2346

我试图调整上面的代码,但没有找到令人满意的解决方案。谁能帮我?

4

2 回答 2

1

使用以下对substitute()函数的调用,而不是问题中列出的整个循环。

substitute(s, '\(\d,\d*\)\@<!\d\ze\(\d\{3}\)\+\d\@!', '&.', 'g')
于 2012-05-06T02:09:04.380 回答
0

试试这个(仅适用于正整数):

查找

(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))

用。。。来代替

,

我还没有尝试过vim它是否有效。但该模式与 PCRE 兼容。


解释

<!--
(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))

Options: case insensitive; ^ and $ match at line breaks

Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=[0-9])»
   Match a single character in the range between “0” and “9” «[0-9]»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=(?:[0-9]{3})+(?![0-9]))»
   Match the regular expression below «(?:[0-9]{3})+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
      Match a single character in the range between “0” and “9” «[0-9]{3}»
         Exactly 3 times «{3}»
   Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?![0-9])»
      Match a single character in the range between “0” and “9” «[0-9]»
-->
于 2012-05-05T10:45:11.527 回答