问题描述
我使用这个免费的,它可以查找和替换。如何搜索所有数字并添加千位分隔符?
输入:< node num ='12345678'>
输出:< node num = '12,345,678'>
可以这样做:
(?< = num ='\d +)(?=(?: \d { 3})+(?!\d))
但是请注意, NET正则表达式,这正是RegExp设计器使用的。大多数正则表达式的风格只允许lookbehinds匹配固定数量的字符。只要有一个明显的最大长度,Java正则表达式就允许可变长度的lookbehinds,所以你可以通过使用 {min,max}
(?
约翰海兰的正则表达式支持向后看。
编辑:我差点忘了;
(num ='\d {1,3} | \G\\ (?:\ d {3})+(?!\ d))
我最喜欢这个纯粹的美学原因。 :)
编辑2:我忘了提及最后一个替换字符串是$ 1,
I'm using this free RegExp Designer which does find and replace. How do I search for all numbers and add thousand separators?
Input: <node num='12345678'>
Output: <node num='12,345,678'>
To reformat numbers only in "num" attribute values you can do this:
(?<=num='\d+)(?=(?:\d{3})+(?!\d))
But note that this will only work in .NET regexes, which is what RegExp Designer uses. Most regex flavors only allow lookbehinds that match a fixed number of characters. Java regexes allow variable-length lookbehinds as long as there's an obvious maximum length, so you can fake it out by using {min,max}
quantifier an arbitrary number for the maximum:
(?<=num='\d{1,20})(?=(?:\d{3})+(?!\d))
John Hyland's regex will work in any flavor that supports lookbehinds.
EDIT: I almost forgot; here's how you can do it without lookbehinds:
(num='\d{1,3}|\G\d{3})(?=(?:\d{3})+(?!\d))
I like this one best for purely aesthetic reasons. :)
EDIT2: I forgot to mention that the replacement string for the last one is "$1,"
这篇关于如何添加千分隔符reg reg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!