仅替换标题中的字符

Replace characters only in headings

我有几个文件(参见输入部分),我需要它用 REGEX 去除 [[ ]],但问题是仅用于:

- # headings
- >quotes

应保留所有其他括号。我无法通过...我正在使用 sublime reader。 如果有人有一些想法,我将非常感激! 谢谢

输入


## [[zoom]] _[v]_

>[[move]] | [[very]] | [[quickly]]

__Synonyms__: [[buzz]] | [[dart]] | [[dash]] | [[dive]] | [[flash]] | [[fly]] | [[hum]] | [[hurtle]] | outstrip | [[rip]] | [[rocket]] | [[rush]] | [[shoot]] | [[shoot]] up | [[skyrocket]] | [[speed]] | [[streak]] | [[surge]] | [[tear]] | [[whirl]] | [[whiz]] | [[zip]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

## [[zip]] _[n]_

>[[enthusiasm]], [[energy]]

__Synonyms__: brio | [[drive]] | [[get]]-upand-[[go]] | [[go]] | [[gusto]] | [[life]] | liveliness | [[oomph]] | [[pep]] | [[pizzazz]] | [[punch]] | [[sparkle]] | [[spirit]] | [[verve]] | [[vigor]] | vim | [[vitality]] | [[zest]] | [[zing]]

__Concepts__: [[LIFE FORMS - GENERAL CHARACTERISTICS#personality (411)\|personality]], [[STATES - ABSTRACT#behavior (633)\|behavior]] 

__Antonyms__: [[apathy]], enervation, [[idleness]], [[laziness]], [[lethargy]]
 
## [[zip]] _[v]_

>[[move]] | [[about]] | [[quickly]]

__Synonyms__: [[bustle]] | [[dash]] | [[flash]] | [[fly]] | [[hasten]] | [[hurry]] | [[run]] | [[rush]] | [[shoot]] | [[speed]] | [[tear]] | waltz | [[whisk]] | [[whiz]] | [[zoom]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

输出


## zoom _[v]_

>move very quickly

__Synonyms__: [[buzz]] | [[dart]] | [[dash]] | [[dive]] | [[flash]] | [[fly]] | [[hum]] | [[hurtle]] | outstrip | [[rip]] | [[rocket]] | [[rush]] | [[shoot]] | [[shoot]] up | [[skyrocket]] | [[speed]] | [[streak]] | [[surge]] | [[tear]] | [[whirl]] | [[whiz]] | [[zip]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

## zip _[n]_

>enthusiasm, energy

__Synonyms__: brio | [[drive]] | [[get]]-upand-[[go]] | [[go]] | [[gusto]] | [[life]] | liveliness | [[oomph]] | [[pep]] | [[pizzazz]] | [[punch]] | [[sparkle]] | [[spirit]] | [[verve]] | [[vigor]] | vim | [[vitality]] | [[zest]] | [[zing]]

__Concepts__: [[LIFE FORMS - GENERAL CHARACTERISTICS#personality (411)\|personality]], [[STATES - ABSTRACT#behavior (633)\|behavior]] 

__Antonyms__: [[apathy]], enervation, [[idleness]], [[laziness]], [[lethargy]]
 
## zip _[v]_

>move about quickly

__Synonyms__: [[bustle]] | [[dash]] | [[flash]] | [[fly]] | [[hasten]] | [[hurry]] | [[run]] | [[rush]] | [[shoot]] | [[speed]] | [[tear]] | waltz | [[whisk]] | [[whiz]] | [[zoom]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

您可以将文本拆分为多行,并映射每行以有条件地进行清理:

function cleanBrackets(text) {
  return text
  .split(/\r?\n/)   // split into lines
  .map((line) => {  // for each line:
    if(line.match(/^[#>]/)) { // if heading or quote:
      line = line.replace(/\[\[([^\]]+)\]\]/g, ''); // cleanup
    }
    return line;
  })
  .join('\n');
}

console.log(cleanBrackets(`## [[zoom]] _[v]_

>[[move]] | [[very]] | [[quickly]]

__Synonyms__: [[buzz]] | [[dart]] | [[dash]] | [[dive]] | [[flash]] | [[fly]] | [[hum]] | [[hurtle]] | outstrip | [[rip]] | [[rocket]] | [[rush]] | [[shoot]] | [[shoot]] up | [[skyrocket]] | [[speed]] | [[streak]] | [[surge]] | [[tear]] | [[whirl]] | [[whiz]] | [[zip]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

## [[zip]] _[n]_

>[[enthusiasm]], [[energy]]

__Synonyms__: brio | [[drive]] | [[get]]-upand-[[go]] | [[go]] | [[gusto]] | [[life]] | liveliness | [[oomph]] | [[pep]] | [[pizzazz]] | [[punch]] | [[sparkle]] | [[spirit]] | [[verve]] | [[vigor]] | vim | [[vitality]] | [[zest]] | [[zing]]

__Concepts__: [[LIFE FORMS - GENERAL CHARACTERISTICS#personality (411)\|personality]], [[STATES - ABSTRACT#behavior (633)\|behavior]] 

__Antonyms__: [[apathy]], enervation, [[idleness]], [[laziness]], [[lethargy]]
 
## [[zip]] _[v]_

>[[move]] | [[about]] | [[quickly]]

__Synonyms__: [[bustle]] | [[dash]] | [[flash]] | [[fly]] | [[hasten]] | [[hurry]] | [[run]] | [[rush]] | [[shoot]] | [[speed]] | [[tear]] | waltz | [[whisk]] | [[whiz]] | [[zoom]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]`));

结果:


## zoom _[v]_

>move | very | quickly

__Synonyms__: [[buzz]] | [[dart]] | [[dash]] | [[dive]] | [[flash]] | [[fly]] | [[hum]] | [[hurtle]] | outstrip | [[rip]] | [[rocket]] | [[rush]] | [[shoot]] | [[shoot]] up | [[skyrocket]] | [[speed]] | [[streak]] | [[surge]] | [[tear]] | [[whirl]] | [[whiz]] | [[zip]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

## zip _[n]_

>enthusiasm, energy

__Synonyms__: brio | [[drive]] | [[get]]-upand-[[go]] | [[go]] | [[gusto]] | [[life]] | liveliness | [[oomph]] | [[pep]] | [[pizzazz]] | [[punch]] | [[sparkle]] | [[spirit]] | [[verve]] | [[vigor]] | vim | [[vitality]] | [[zest]] | [[zing]]

__Concepts__: [[LIFE FORMS - GENERAL CHARACTERISTICS#personality (411)|personality]], [[STATES - ABSTRACT#behavior (633)|behavior]] 

__Antonyms__: [[apathy]], enervation, [[idleness]], [[laziness]], [[lethargy]]
 
## zip _[v]_

>move | about | quickly

__Synonyms__: [[bustle]] | [[dash]] | [[flash]] | [[fly]] | [[hasten]] | [[hurry]] | [[run]] | [[rush]] | [[shoot]] | [[speed]] | [[tear]] | waltz | [[whisk]] | [[whiz]] | [[zoom]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

我认为有一种更简单的方法可以做到这一点。您只需要检查该行是否以 ##> 字符串开头。

下面的代码是python.

我是这样做的:

for line in my_file_data.split('\n'):
    if line.startswith(('##','>')):
        line = line.replace('[[','').replace(']]','')
    print (line)

我把所有的原始数据存入了my_file_data。然后拆分得到每一行。

这个输出是:

## zoom _[v]_

>move | very | quickly

__Synonyms__: [[buzz]] | [[dart]] | [[dash]] | [[dive]] | [[flash]] | [[fly]] | [[hum]] | [[hurtle]] | outstrip | [[rip]] | [[rocket]] | [[rush]] | [[shoot]] | [[shoot]] up | [[skyrocket]] | [[speed]] | [[streak]] | [[surge]] | [[tear]] | [[whirl]] | [[whiz]] | [[zip]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

## zip _[n]_

>enthusiasm, energy

__Synonyms__: brio | [[drive]] | [[get]]-upand-[[go]] | [[go]] | [[gusto]] | [[life]] | liveliness | [[oomph]] | [[pep]] | [[pizzazz]] | [[punch]] | [[sparkle]] | [[spirit]] | [[verve]] | [[vigor]] | vim | [[vitality]] | [[zest]] | [[zing]]

__Concepts__: [[LIFE FORMS - GENERAL CHARACTERISTICS#personality (411)\|personality]], [[STATES - ABSTRACT#behavior (633)\|behavior]] 

__Antonyms__: [[apathy]], enervation, [[idleness]], [[laziness]], [[lethargy]]
 
## zip _[v]_

>move | about | quickly

__Synonyms__: [[bustle]] | [[dash]] | [[flash]] | [[fly]] | [[hasten]] | [[hurry]] | [[run]] | [[rush]] | [[shoot]] | [[speed]] | [[tear]] | waltz | [[whisk]] | [[whiz]] | [[zoom]]

__Concepts__: [[ACTIONS - MOTION#move oneself quickly (150)\|move oneself quickly]] 

__Antonyms__: decelerate, [[slow]]

在 sublime 文本编辑器中,您可以使用正则表达式的 PCRE 风格。以下内容应该适合您。

搜索正则表达式:

(?:^[#>]|(?!^)\G).*?\K(?:\[\[|]])

替换为空字符串。

RegEx Demo

正则表达式详细信息:

  • (?:: 启动非捕获组
    • ^[#>]:匹配#>开头
    • |: 或
    • (?!^)\G: 从上一场比赛结束重新开始比赛
  • ):结束非捕获组
  • .*?:匹配0个或多个任意字符(惰性)
  • \K: 重置匹配信息
  • (?:\[\[|]]):匹配[[]]

以下似乎有效:

^_.*(*SKIP)(*F)|\[\[|]]
  • ^ - 开始字符串锚点。
  • _.* - 匹配下划线和除换行符之外的 0+ 个其他字符。
  • (*SKIP)(*F) - 将控制动词回溯到 (*F) 对于 (*FAIL) 的 shorthand。这种控件组合基本上表示:"Throw away anything you can match to the left of me."
  • | - 或者:
  • \[\[ - 双字面开括号。
  • | - 或者:
  • ]] - 双字面右括号。

在线查看demo


要使其更具限制性,您可以使用:

^[^#>\n].*(*SKIP)(*F)|\[\[|]]

唯一的区别是否定字符 class 确保我们不只是查找以下划线开头的行。


之前:

之后: