Replace characters only in headings

Viewed 69

I have several files (see the Input section), what I need it to strip [[ ]] with REGEX, but the problem is that only for:

- # headings
- >quotes

All other brackets should be preserved. I cannot make it thru... I'm using the sublime reader. If anybody have some ideas I will really appreciate it! Thanks

Input


## [[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]]

Output


## 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]]
4 Answers

In sublime text editor you can use PCRE flavor of regex. Following should work for you.

Search regex:

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

Replace with an empty string.

RegEx Demo

RegEx Details:

  • (?:: Start non-capture group
    • ^[#>]: Match # or > at the start
    • |: OR
    • (?!^)\G: Restart matching from end of the previous match
  • ): End non-capture group
  • .*?: Match 0 or more of any character (lazy)
  • \K: Reset match info
  • (?:\[\[|]]): Match [[ or ]]

The following seems to work:

^_.*(*SKIP)(*F)|\[\[|]]
  • ^ - Start string anchor.
  • _.* - Match an underscore and 0+ other character than newline.
  • (*SKIP)(*F) - Backtracking control verbs to where (*F) is shorthand for (*FAIL). This combination of controls basically says: "Throw away anything you can match to the left of me."
  • | - Or:
  • \[\[ - A double literal open bracket.
  • | - Or:
  • ]] - A double literal closing bracket.

See the online demo


To make this more restrictive you can use:

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

Where the only diference is that the negated character class makes sure we not just looking for lines starting with an underscore.


Before:

enter image description here

After:

enter image description here

I think there is a much easier way to do this. You just need to check if the line startswith the ## or > string.

The below code is python.

Here's how I did it:

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

I stored all the raw data into my_file_data. Then did a split to get each line.

The output of this is:

## 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]]

You can split the text into lines, and map through each line to do the cleanup conditionally:

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, '$1'); // 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]]`));

Result:


## 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]]
Related