最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】VSCode中如何使用正则表达式去替换且被替换中使用分组group

VSCode crifan 9747浏览 0评论

想要通过VSCode去从:

<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d3a.mp3">e10d3a.mp3</a> 2015-09-23 09:10 9.3M
<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d3b.mp3">e10d3b.mp3</a> 2015-09-23 09:10 7.0M
<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d5a.mp3">e10d5a.mp3</a> 2015-09-23 09:11 54M
...

的内容中,提取出:

e10d3a.mp3

e10d3b.mp3

e10d5a.mp3

如图:

一是:用:

^<img src="/icons/sound2\.gif" alt="\[SND\]"> <a href="\w+\.mp3

没有匹配到mp3的部分

二是:替换后的内容,想要调用使用分组group中的mp3的字符串

类似于:

^<img src="/icons/sound2\.gif" alt="\[SND\]"> <a href="(\w+\.mp3)
(1)

搜:

vscode 正则

如何使用vscode正则表达式批量替换递增的行标 – CRIMX的回答 – SegmentFault 思否

替换后用:

$N

去表示对应的group中的内容

在 Visual Studio 中使用正则表达式

vscode插件:正则表达式预览 | le0zh’s blog

vscode中的正则搜索与替换演示 – CSDN博客

vscode regex replace

visual studio code – VSCode regex find & replace submatch math? – Stack Overflow

Cannot reference regex group for find and replace in Search Panel · Issue #9586 · Microsoft/vscode

Find and replace capture group/backreference inserts `undefined` instead of empty string · Issue #19740 · Microsoft/vscode

Cannot Prepend RegEx Match Group Replacement with a $ · Issue #28146 · Microsoft/vscode

去试试:

^[^\r\n]+href="(\w+\.mp3)"[^\r\n]+$
$1

是可以实现要的效果的,都换成了对应的xxx.mp3了:

另外,关于具体的语法的支持,从:

Basic Editing in Visual Studio Code

VS Code does support regular expression searches, however, backreferences, lookaround, and multiline matches are not supported. This is because VS Code depends on the search tool ripgrep, which, while extremely fast, doesn’t support these advanced regex features.

->

BurntSushi/ripgrep: ripgrep recursively searches directories for a regex pattern

->

regex – Rust

里面有详细的介绍

另外,里面没有详细的解释说,replacement中用$N去引用匹配的第N个group

但是通过:

let re = Regex::new(r"(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})").unwrap();
let before = "2012-03-14, 2013-01-01 and 2014-07-05";
let after = re.replace_all(before, "$m/$d/$y");
assert_eq!(after, "03/14/2012, 01/01/2013 and 07/05/2014");

可以看出的确是这么用的

且对于named group来说,查找时正则中named group的写法,和Python类似也是:

(?P<yourGroupName>xxxx)

然后用:

$yourGroupName

去引用。

去试试

^[^\r\n]+href="(?P<audioFilename>\w+\.mp3)"[^\r\n]+$
$audioFilename

结果直接红色提醒,不支持这个语法-》没法使用namded group

但是看到:

https://docs.rs/regex/0.2.5/regex/#syntax

中有说:

(exp) numbered capture group (indexed by opening parenthesis)
(?Pexp) named (also numbered) capture group (allowed chars: [_0-9a-zA-Z])

看来VSCode中不支持?

但是支持:

(?:exp) non-capturing group

^[^\r\n]+href="(?:\w+\.mp3)"[^\r\n]+$

-》所以目前看来暂时只能使用numbered group了

用:

^[^\r\n]+href="(\w+\.mp3)"[^\r\n]+$
$1

<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d3a.mp3">e10d3a.mp3</a> 2015-09-23 09:10 9.3M 
<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d3b.mp3">e10d3b.mp3</a> 2015-09-23 09:10 7.0M 
<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d5a.mp3">e10d5a.mp3</a> 2015-09-23 09:11 54M
...

再去点击:

全部替换

替换为:

进一步的,其实此处是为了得到批量的mp3的下载地址,供后续迅雷去批量下载的

所以再去给替换后的mp3加上地址前缀:

http://media.talkbank.org/CHILDES/Biling/Singapore/

正则:

^[^\r\n]+href="(\w+\.mp3)"[^\r\n]+$
http://media.talkbank.org/CHILDES/Biling/Singapore/$1

把:

<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d3a.mp3">e10d3a.mp3</a> 2015-09-23 09:10 9.3M 
<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d3b.mp3">e10d3b.mp3</a> 2015-09-23 09:10 7.0M 
<img src="/icons/sound2.gif" alt="[SND]"> <a href="e10d5a.mp3">e10d5a.mp3</a> 2015-09-23 09:11 54M

 

替换为:

http://media.talkbank.org/CHILDES/Biling/Singapore/e10d3a.mp3



 

注意:

VSCode中的正则利用的是第三方的库:

BurntSushi/ripgrep: ripgrep recursively searches directories for a regex pattern

虽然其语法介绍中说是支持named capture group=命名的捕获的组=可以给组命名

比如:(?P<yourGroupName>xxx) -> $yourGroupName

但是实际上不支持,只能用numbered capture group=数组捕获的组

比如:(xxx) -> $1

然后就可以利用迅雷去批量下载mp3了:

转载请注明:在路上 » 【已解决】VSCode中如何使用正则表达式去替换且被替换中使用分组group

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.162 seconds, using 22.15MB memory