0

I am trying to replace all occurrences of Secured="*" with Secured="testValue"

int testValue = 9;
string text = "Test blah Secured=\"6\" Test blah Secured=\"3\" ";

 Regex r = new Regex("Secured=\".*\"  ");
text = r.Replace(text, "Secured=\" \" + newValue.toString() + \"\" ");

Here is my method, the issue is it changes nothing?

    [Published]
    public string GetConvertedVdiXML(string myText, int newValue)
    {
        string text = myText;
        Regex r = new Regex("Secured=\".*\"  ");
        text = r.Replace(text, "Secured=\" " + newValue.ToString() + " \" ");

        return text;
    }

The issue is it is not updating?

3
  • 1
    What do you mean by "not updating"? Is the returned value not what you were expecting? Commented Mar 23, 2015 at 11:53
  • GetConvertedVdiXML - If this is XML there are better ways than RegExp Commented Mar 23, 2015 at 12:19
  • Of course it matches nothing, your regex pattern is looking for Secured=".*" followed by 2 spaces. That pattern does not exist in your input string. Every instance of Secured="*" in your input is only followed by a single space. Commented Mar 23, 2015 at 12:34

1 Answer 1

1

You should use

    text = r.Replace(text, "Secured=\" \"" + newValue.ToString() + "\" ");

Since the newValue is a variable. It cannot be part of a string. The updated code should work:

int newValue = 9;
string text = "Test blah Secured=\"6\" Test blah Secured=\"3\" ";
Regex r = new Regex(@"Secured=""[^""]*"" ");
text = r.Replace(text, "Secured=\"" + newValue.ToString() + "\" ");

Output: Test blah Secured="9" Test blah Secured="9"

Also, here is the function code:

public string GetConvertedVdiXML(string myText, int newValue)
{
    string text = myText;
    Regex r = new Regex(@"Secured=""[^""]*"" ");
    text = r.Replace(text, "EditableBy=\"" + newValue.ToString() + "\" ");
    return text;
}
Sign up to request clarification or add additional context in comments.

1 Comment

The returned string has nothing changed? example string could be: <TEST1 Secured="0" /> <TEST2 Secured="0"/> so the " might not have \ before them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.