How can I achieve the following using Regex/c# replace?
Input string I have : ” test data : <test param="p" value="v"/> input string continues”
Output string I need : ” test data : ((test param={p} value={v})) input string continues “
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume your real question is HOW to achieve it, and find/replace requirements seem to be fairly rigid, here is one that works for your case:
The Find expression can be broken down like this:
The parentheses
()in the regex mean to “capture” any characters matched by the contents of the parentheses. Anything not in parentheses will be “matched”, but will be discarded during a find and replace. This is useful for extracting bits of data from a pattern like extractingname,attribute1andvalue1from<name attribute1="value1"/>to then put into another pattern of text.In C#, you use the System.Text.RegularExpressions.Regex object to match or to replace using regular expressions. I believe the syntax/signature (for an instance of the Regex object) is
regexObject.Replace(input As String, replacement As String) As StringThe replacement expression contains
$1,$2, etc, which refer to the parts of the match enclosed in parantheses(). Thus,$1(in the replacement expression) will insert the text matched by the first group([^:]*)(from the match expression)This combination will turn this text:
Into this text:
A great resource for learning about Regexes (and where I learned about 90% of what I know) is Regular-Expressions.info, and an associated tool called RegexBuddy is great for constructing, testing, and debugging regexes as well.