简体   繁体   中英

Extract String between 2 dollar sign

I have string which contains a variable. But i need to replace the name with what i have in the db.

string text = "hello $$name$$, good morning"

How can i extract the name by using Regex ?

This only works if i have single $

var MathedContent = Regex.Match((string)bodyObject, @"\\$.*?\\$");

You could define regular expression, "(\\$\\$)(.*?)(\\$\\$)" with 3 different groups:

 "(\$\$)(.*?)(\$\$)"
 ^^^^^^|^^^^^|^^^^^^
    $1    $2    $3

and then if you need just simple replacement you can do something like this:

string replacedText = Regex
    .Replace("hello $$name$$, good morning", @"(\$\$)(.*?)(\$\$)", "replacement");
//hello replacement, good morning

or combine with the other groups

string replacedText = Regex
    .Replace("hello $$name$$, good morning", @"(\$\$)(.*?)(\$\$)", "$1replacement$3");
//hello $$replacement$$, good morning

On the other hand, if you need more control you could do something like this(tnx to Wiktor ):

IDictionary<string, string> factory = new Dictionary<string, string>
{
    {"name", "replacement"}
};

string replacedText = Regex.Replace(
    "hello $$name$$, good morning",
    @"(?<b>\$\$)(?<replacable>.*?)(?<e>\$\$)",
    m => m.Groups["b"].Value + factory[m.Groups["replacable"].Value] + m.Groups["e"].Value);
//hello $$replacement$$, good morning

Your question is slightly ambigous as to whether you want to replace the entire $$name$$ or find the string between the dollars.

Here's working code for both:

Replace $$name$$ with Bob

    string input = "hello $$name$$, good morning";
    var replaced = Regex.Replace(input, @"(\$\$\w+\$\$)", "Bob");
    Console.WriteLine($"replaced: {replaced}");

Prints replaced: hello Bob, good morning

Extract name from string:

    string input = "hello $$name$$, good morning";
    var match = Regex.Match(input, @"\$\$(\w+)\$\$").Groups[1].ToString();
    Console.WriteLine($"match: {match}");

Prints match: name

If you want to capture the text between the $$ delimiters but exclude the $$ themselves, you can use lookaround : (?<=\\$\\$).*?(?=\\$\\$)

Lookarounds are zero-length assertions (much like \\b ) that match characters but do not include them in the result. (?<=XXX)YYY matches YYY on condition that it's preceded by XXX . Similarly, YYY(?=ZZZ) matches YYY on condition that it's followed by ZZZ .

var match = Regex.Match("hello $$name$$, good morning", @"(?<=\$\$).*?(?=\$\$)");
Console.WriteLine(match.Value);   // outputs "name"
string input = "hello $$name$$, good morning";
Regex rx = new Regex(@"(?<=\$\$)(.*?)(?=\$\$)");

Console.WriteLine(rx.Match(input).Groups[1].Value); 

Result:

name

Use the negation set, [^ ] . Such as [^$]+ where we will match until the next $ .

string text = "hello $$name$$, good morning";

Regex.Replace(text, @"\$\$[^$]+\$\$", "Jabberwocky");

Result:

 hello Jabberwocky, good morning

More verbose but easier to read without escaping, pattern [$]{2}[^$]+[$]{2} .

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