簡體   English   中英

正則表達式替換第二次出現的字符

[英]Regex to replace second occurrence of a character

我有以下格式的一些數據:

MM:ss:mmm其中MM is minutesss is secondsmmm is 3 digit milliseconds ,如:

05:23:236

我正在嘗試用點替換第二次出現的冒號:

05:23.236

我想使用regex模式在像Notepad ++這樣的編輯器中進行替換,我想出了這個正則表達式來匹配我的表達式:

 \d{1,2}:\d{1,2}:\d{1,3}

但是現在我怎么才能得到second occurrence of colon所以我可以用dot替換它?

編輯:請注意,我正在使用的數據可能有1-2位數分鍾,1-2位數字和1-3位數毫秒

使用這個正則表達式:

:(\d{1,3})$

替換為:

.$1

上面做的是選擇最后一個:后跟毫秒乘以1-3位數。

DEMO

嘗試這個:

  string pattern =  @":(?=\d{3})";
  string input = "your string";
  string replacement = ".";
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);

你的正則表達式是好的,你只需要做組,然后通過再次調用它們來替換它們:

選擇:

(\d{1,2}:\d{1,2}):(\d{1,3})

更換:

$1\.$2

查找:: :(\\d{1,3})$

替換為: .\\1.$1取決於您的正則表達式風格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM