2017-08-04 9 views
1

を繰り返し交換する正規表現:1321654987.00 結果は次のようになります。1,321,654,987.00PowerShellは、私は、この文字列にカンマを追加する必要がグループ

私は置換を使用しようとしている:

'123123897.00' -replace '^(?<start>(\d{1,3}))(?<mid>(\d{3}))*(?<end>(\.\d{2}))?$','${start},${mid}${end}' 

しかし、結果は以下の通りです: 1,987.00

最後に一致したグループではなく、一致するグループをどのように置き換えることができますか?

ありがとうございます!

+3

私はあなたがこれに間違ったアプローチをしていると思います。 [.NET数値書式](https://technet.microsoft.com/en-us/library/ee692795.aspx)がうまく機能しますか? –

+0

私は '$ s -replace '(?<!\ .. *)\ B(?=(?:\ d {3})+(?:\。\ d +)?$)'、動作しますが、マイクは正しい、正規表現はあなたが本当に必要なものではありません。 –

答えて

2

正規表現は言われて、マイクはあなたが書式設定機能を使用してする必要があることを権利であることを、この

'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. 

のようなものである可能性があります。以下は十分です。

"{0:N2}" -f 123123897.00 
関連する問題