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 "
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 "
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:
Find: ([^:]*): <([^=]*)="([^"])"([^=]*)="([^"])"/>
Replace: $1: (($2={$3}$4={$5}))
The Find expression can be broken down like this:
([^:]*) # Capture zero or more characters that ARE NOT a colon
: < # Match a colon, a literal space, then a less-than sign
([^=]*) # Capture zero or more characters that ARE NOT an equals sign
=" # Match an equals sign, then a double quote
([^"]) # Capture zero or more characters that ARE NOT a double quote
" # Match a double quote
([^=]*) # Capture zero or more characters that ARE NOT an equals sign
=" # Match an equals sign, then a double quote
([^"]) # Capture zero or more characters that ARE NOT a double quote
"/> # Match a double quote, a forward slash, then a greater-than sign
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 extracting name, attribute1 and value1 from <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 String
The 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:
"test data : <test param="p" value="v"/>" more text. blah blah blah "test data : <test param="p" value="v"/>" and then some more text...
Into this text:
"test data : ((test param={p} value={v}))" more text. blah blah blah "test data : ((test param={p} value={v}))" and then some more 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.
$1 in the third paragraph (which starts The replacement expression contains...), and I'm not sure if that's not clear or if it's missing something. Without going pretty deep into regex internals, I'm not sure what else I can add :D or were you asking more about the match part ([^:]*):?$ - every other character is literal. I'll add a little more to the answer