简体   繁体   中英

How does $' in a regex works?

Problem

At http://regex101.com , it is said that '$ will return a portion of the source string that follows the match. Yet it doesn't work like this for me.

Example

For example, I need to replace text in JSON. I need to remove some text from JSON after certain parameters.

 "ReportPackage": {
        "$id": "some id",
        "$type": "stuff",
        "ConnectionString": "REPLACE_HERE",
        "FtpConfiguration": {
            "$id": "some id",
            "$type": "some type",
            "Address": "some adress",
            "Password": "REPLACE_HERE",
            "Username": "REPLACE_HERE",
            "BaseDirectory": "some path"
        },
        "PgConnectionString": "REPLACE_HERE"
    }
},
"CurrentFederation": "",
"CurrentVirtualHost": "/",
"PgConnectionString": "REPLACE_HERE",
"PeriodicContinuationActivated": true

I need to replace the values marked as REPLACE_HERE . I can use four separate regexes but I want to use single one(if possible).

My Attempt to Solve the Problem

I use this regex $'((("Password")|("ConnectionString")|("PgConnectionString")|("Username")))(.*), yet it doesn't work as I imagined is would. What did I do wrong?

You misunderstood the .NET regex reference . In fact, $' is a valid substituion pattern, and it cannot be used to do what you want in the regex pattern.

$' Includes all the text of the input string after the match in the replacement string.

In your $'((("Password")|("ConnectionString")|("PgConnectionString")|("Username")))(.*), pattern, $ simply requires the end of string position and since there are more patterns after $ , the regex will never match any string.

More, JSON strings cannot be safely manipulated with regex without assumptions .

If your values do not contain double quotes, and if the JSON string is indented the way you showed you could try

Regex.Replace(text, @"(?m)^(\s*""(?:Password|ConnectionString|PgConnectionString|Username)""\s*:\s*"")[^""]+", "$1")

where

  • (?m) - makes ^ match start of line positions
  • ^ - start of a line
  • (\s*""(?:Password|ConnectionString|PgConnectionString|Username)""\s*:\s*"") - Group 1 that captures
    • \s* - zero or more whitespaces
    • " - a " char
    • (?:Password|ConnectionString|PgConnectionString|Username) - one of the strings listed
    • "\s*:\s*" - " , a : enclosed with zero or more whitespaces and then a "
  • [^"]+ - one or more chars other than " .

The $1 replacement pattern replaces the matches with Group 1 value.

See the regex demo .

To do that properly, parse JSON string and clear out the key values you need. See How can I parse JSON with C#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM