简体   繁体   English

正则表达式替换第二次出现的字符

[英]Regex to replace second occurrence of a character

I have some data in the following format: 我有以下格式的一些数据:

MM:ss:mmm where MM is minutes , ss is seconds and mmm is 3 digit milliseconds , like: MM:ss:mmm其中MM is minutesss is secondsmmm is 3 digit milliseconds ,如:

05:23:236

I'm trying to replace the second occurrence of the colon with a dot: 我正在尝试用点替换第二次出现的冒号:

05:23.236

I'd like to use a regex pattern to do a replace in an editor like Notepad++, I came up with this regex to match my expression: 我想使用regex模式在像Notepad ++这样的编辑器中进行替换,我想出了这个正则表达式来匹配我的表达式:

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

But now how can I get only the second occurrence of colon so I can replace it with dot ? 但是现在我怎么才能得到second occurrence of colon所以我可以用dot替换它?

EDIT: Notice that the data I'm working with could come with 1-2 digit minute, 1-2 digit second and 1-3 digit millisecond 编辑:请注意,我正在使用的数据可能有1-2位数分钟,1-2位数字和1-3位数毫秒

Use this regex: 使用这个正则表达式:

:(\d{1,3})$

to replace with: 替换为:

.$1

What above is doing is selecting the last : which is followed by milliseconds by 1-3 digits. 上面做的是选择最后一个:后跟毫秒乘以1-3位数。

DEMO DEMO

Try this: 尝试这个:

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

your regex was good, you just have to do group and then replace them by calling them again: 你的正则表达式是好的,你只需要做组,然后通过再次调用它们来替换它们:

selection: 选择:

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

replace: 更换:

$1\.$2

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

Replace with: .\\1 or .$1 depending on your regex flavour 替换为: .\\1.$1取决于您的正则表达式风格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM