Powershell 正则表达式替换重复组
Powershell regex replace repeating groups
我必须在这个字符串中添加逗号:1321654987.00
结果应该是:1,321,654,987.00
我正在尝试使用替换:
'123123897.00' -replace '^(?<start>(\d{1,3}))(?<mid>(\d{3}))*(?<end>(\.\d{2}))?$','${start},${mid}${end}'
但结果如下:
1,987.00
如何替换每个匹配的组,而不是最后一个?
提前致谢!
正则表达式可能是这样的
'123123897.00' -replace '(?m)(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))', ','
说明
# (?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))
#
# Options: Case sensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks; Parentheses capture
#
# 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]»
# Your regular expression may find zero-length matches
# PowerShell allows a zero-length match at the position where the previous match ends.
# PowerShell advances one character through the string before attempting the next match if the previous match was zero-length.
话虽这么说,Mike 是对的,您应该使用格式化函数。以下就够了
"{0:N2}" -f 123123897.00
我必须在这个字符串中添加逗号:1321654987.00 结果应该是:1,321,654,987.00
我正在尝试使用替换:
'123123897.00' -replace '^(?<start>(\d{1,3}))(?<mid>(\d{3}))*(?<end>(\.\d{2}))?$','${start},${mid}${end}'
但结果如下: 1,987.00
如何替换每个匹配的组,而不是最后一个?
提前致谢!
正则表达式可能是这样的
'123123897.00' -replace '(?m)(?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))', ','
说明
# (?<=[0-9])(?=(?:[0-9]{3})+(?![0-9]))
#
# Options: Case sensitive; Exact spacing; Dot doesn't match line breaks; ^$ match at line breaks; Parentheses capture
#
# 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]»
# Your regular expression may find zero-length matches
# PowerShell allows a zero-length match at the position where the previous match ends.
# PowerShell advances one character through the string before attempting the next match if the previous match was zero-length.
话虽这么说,Mike 是对的,您应该使用格式化函数。以下就够了
"{0:N2}" -f 123123897.00