Alex P
Alex P

Reputation: 119

Remove certain part of text files with VSCode's regex search & replace

I have hundreds of text files with contents of this type (these are .txt content files from Kirby CMS):

Attributes:

- attribute: ''

----

Materials:

- 
  material: ""
  price: 0
  available: 'true'
  default: 'true'
  material_variations:
    - 
      variations:
        - 
          option: 
          variation: —
          price: "0"
          variation_available: 'true'
        - 
          option: 
          variation: ""
          price: "0"
          variation_available: 'true'

----

Price: 0

I want to remove the part from Materials: to the next ----, so the above snippet becomes:

Attributes:

- attribute: ''

----

Price: 0

I am searching for a regular expression to achieve that, but some things that I thought could maybe work (e.g. \bMaterials\b[^]\b----\b), produce an error in VSCode.

Upvotes: 2

Views: 922

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

The [^] construct only works in the Find and Replace in-document search tool. In the file search tool, Rust regex flavor is used, and [^] does not work there.

You can use

\bMaterials\b[\w\W]*?----\n*

Details:

  • \bMaterials\b - a whole word Materials
  • [\w\W]*? - any zero or more chars as few as possible
  • ---- - a literal string of four hyphens
  • \n* - zero or more line breaks.

Upvotes: 1

Related Questions