C# regex extracting tags
Say I have a few string like
Hi my name is &开发者_开发百科lt;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..
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>
"</?[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
}
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
精彩评论