使用正则表达式替换时,如何保留匹配字符串的一部分? [英] While replacing using regex, How to keep a part of matched string?

查看:144
本文介绍了使用正则表达式替换时,如何保留匹配字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

12.hello.mp3
21.true.mp3
35.good.mp3
.
.
.

文本文件中列出的文件名.

so on as file names in listed in a text file.

我只需要用空格替换数字前面的那些点(.).(例如 12.hello.mp3 => 12 hello.mp3).如果我将正则表达式设为[0-9].",它也会替换数字.请帮帮我.

I need to replace only those dots(.) infront of numbers with a space.(e.g. 12.hello.mp3 => 12 hello.mp3). If I have regex as "[0-9].", it replaces number also. Please help me.

推荐答案

替换

^(\d+)\.(.*mp3)$

\1 \2

此外,在最新版本的 notepad++ 中,它还将接受以下内容,其他 IDE/编辑器(例如 Intellij IDEA 等 JetBrains 产品)也接受以下内容:

Also, in recent versions of notepad++, it will also accept the following, which is also accepted by other IDEs/editors (eg. JetBrains products like Intellij IDEA):

$1 $2

这里假设 notepad++ 正则表达式匹配引擎支持组.正则表达式的基本意思是:将第一个点前面的数字作为第 1 组,将其后的所有数字作为第 2 组(但前提是它以 mp3 结尾)

This assumes that the notepad++ regex matching engine supports groups. What the regex basically means is: match the digits in front of the first dot as group 1 and everything after it as group 2 (but only if it ends with mp3)

这篇关于使用正则表达式替换时,如何保留匹配字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆