Home > Software design >  How do I select initial and final characters through regex and replace them?
How do I select initial and final characters through regex and replace them?

Time:03-12

I have 100s of files which contains the following code:

color: $variable

I wish to replace all of them to

color: var(--variable)

So, essentially, replacing $ with var(-- ). How do I accomplish this?

CodePudding user response:

Here is one way to do so:

Find:

(?<=color: )\$(. )

Replace with:

var(--$1)

  • (?<=): Positive lookbehind.
  • color: : Matches color: .
  • \$: Matches $.
  • (. ): Matches any character except new lines, between one and unlimited times, as much as possible.

  • $1 is the first capture group (first match in parentheses).

CodePudding user response:

Capture the parts you want to keep then put them back using back references:

   Find: color: \$(\w )
Replace: color: var(--$1)
  • Related