[英]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 minutes
, ss is seconds
, mmm 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位数毫秒
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.