Nav Malik
Nav's Blog

Nav's Blog

using regex to replace values of a certain variable  all files in a folder

using regex to replace values of a certain variable all files in a folder

Nav Malik's photo
Nav Malik
·Nov 22, 2022·

1 min read

What is a Regular Expression?

On an abstract level a regular expression, regex for short, is a shorthand representation for a set. A set of strings.

What are Regular Expressions used for?

Regular expressions are useful in any scenario that benefits from full or partial pattern matches on strings. These are some common use cases:

  • verify the structure of strings
  • extract substrings form structured strings
  • search / replace / rearrange parts of the string
  • split a string into tokens

Scenario

Let's assume you want to remove all the occurrences of the variable xyz in all files in a certain directory, you would use grepWin software to perform the regex search on the entire directory.

For instance, to replace below occurrence of variable (there is a blank line after the value abc)

xyz:
  - abc

you will need to use

xyz:[\n\r](\s\s-.)([^\s]+)[\n\r]

to replace below occurrence of variable (no blank line after value abc)

xyz:
  - abc

you will need to use

xyz:[\n\r](\s\s-.)([^\s]+)

to replace below occurrence of variable

xyz: abc

you will need to use

xyz:(\s.)([^\s]+)
 
Share this