Search code examples
regexpowershell

Regex Powershell: find a string, then replace nth occurence of another string


I have 200 files with lines of text, that could look like any of these paterns:

{hello} "this is an example" {{ config(alias="customertable") }}
{{hello}} "this is an example" {{ config ( alias = 'monthly revenue') }}
{ { config ( alias = 'record 3 breaking news') } } {{hello}} "this is an example"
{{hello}} 'this is an example' { { config( alias = "designer bags 4 u") }} {{hello}} "this is an example"

The string after the word alias is always in single or double quotes. I want to put _link at the end of that string, so it looks like this: "customertable_link" or 'monthly revenue_link' and want to use powershell. I want the whole line to be returned, so the forth example should return this:

{{hello}} 'this is an example' { { config( alias = "designer bags 4 u_link") }} {{hello}} "this is an example"

Eg, search for the string alias on a line, then add _link to the left of the second instance of " or '.

This is what I have so far:

$string -replace 'alias=(?:[^"]*"){2}', '$0_link'

And that works almost for the first example (only the quote is in the wrong place), but I am not able to get it right. Any help would be greatly appreciated!


Solution

  • You can use

    \b(alias\s*=\s*(["'])(?:(?!\2).)*)\2
    

    and replace with $1_link$2.

    See the regex demo.

    In Powershell, it will look like

    $rx = '\b(alias\s*=\s*(["''])(?:(?!\2).)*)\2'
    $string -replace $rx, '$1_link$2'
    

    Details:

    • \b - a word boundary
    • (alias\s*=\s*(["'])(?:(?!\2).)*) - Group 1:
      • alias - a word
      • \s*=\s* - a = char enclosed within zero or more whitespaces
      • (["']) - Group 2: a " or '
      • (?:(?!\2).)* - any zero or more chars other than newline char as many as possible, that are not equal to the char captured into Group 2
    • \2 - either " or ' captured into Group 2