0

Say I have a few string like

Hi my name is <Name>
Hi <name>, shall we go for a <Drink>

Is it possible to get the tags captured through c# Regex? like <Name>, <drink> etc? I am not able to get it right..

3 Answers 3

2

Sure:

Regex.Matches(myString, "<([^>]+)>");

PowerShell Example:

PS> $s = @'
>> Hi my name is <Name>
>> Hi <name>, shall we go for a <Drink>
>> '@
>>
PS> [regex]::Matches($s,  '<([^>]+)>') | ft

Groups                Success Captures      Index      Length Value
------                ------- --------      -----      ------ -----
{<Name>, Name}           True {<Name>}         14           6 <Name>
{<name>, name}           True {<name>}         25           6 <name>
{<Drink>, Drink}         True {<Drink>}        51           7 <Drink>
Sign up to request clarification or add additional context in comments.

Comments

1
"</?[a-z][a-z0-9]*[^<>]*>"

To use it, try something like this:

try
{
    Regex regexObj = new Regex("</?[a-z][a-z0-9]*[^<>]*>", RegexOptions.IgnoreCase);
    Match matchResults = regexObj.Match(subjectString);
    while (matchResults.Success)
    {
        // Do Stuff

        // matched text: matchResults.Value
        // match start: matchResults.Index
        // match length: matchResults.Length
        matchResults = matchResults.NextMatch();
    } 
}
catch (ArgumentException ex)
{
    // Syntax error in the regular expression
}

Comments

0

Why not do something even simpler like using C#s String.Replace? You pass in the string to be replaced and give it whatever value you want to replace it with.

See here for examples: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

1 Comment

thanks for your response. I cannot do it the way you suggensted, because the values are in a DB

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.