none
Regex.Replace with alternate outputs RRS feed

  • Question

  • Regex.Replace seems extra difficult for me to grasp. How do I substitute alternate inputs to get alternate outputs?

    string input = " PT";

    string output = Regex.Replace(input, pattern, replacement);

    expected output = "Y"

    However if the intput string is anything else (or I could specify just a space as the other input) the expected output is "N"

    perhaps pattern should be "( PT| )" but I am probably wrong.

    I have no idea what replacement should be in order to get "Y" for one input and "N" for the other.

    Of course this is easy in C# code, but the solution needs to be Regex if at all possible.

    Thanks


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.

    Tuesday, July 15, 2014 2:52 PM

Answers

  • Hello,

    >>Regex.Replace seems extra difficult for me to grasp. How do I substitute alternate inputs to get alternate outputs?

    This is just impossible with only Regex because the Regex does not syntax like if condition A return B else return C. It is an expression to be used to check whether the input string has a sub string which is matched the regex expression.

    We need to do it with C # syntax, a way looks like:

    string toReplace = "P T";
    
    string replaced = Regex.Replace(toReplace, "PT", "Y") == "Y" ? "Y" : "N";
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jon Q Jacobs Wednesday, July 16, 2014 11:27 AM
    Wednesday, July 16, 2014 3:36 AM
    Moderator

All replies

  • Hello,

    >>Regex.Replace seems extra difficult for me to grasp. How do I substitute alternate inputs to get alternate outputs?

    This is just impossible with only Regex because the Regex does not syntax like if condition A return B else return C. It is an expression to be used to check whether the input string has a sub string which is matched the regex expression.

    We need to do it with C # syntax, a way looks like:

    string toReplace = "P T";
    
    string replaced = Regex.Replace(toReplace, "PT", "Y") == "Y" ? "Y" : "N";
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Jon Q Jacobs Wednesday, July 16, 2014 11:27 AM
    Wednesday, July 16, 2014 3:36 AM
    Moderator
  • That's what I needed to know, that Regex alone can't do it.

    Thank you very much.


    Jon Jacobs
    In transmission, subatomic particles managed by professionals.
    No innocent electrons were harmed.

    Wednesday, July 16, 2014 11:29 AM