说我有喜欢C#正则表达式提取标签
Hi my name is <Name>
Hi <name>, shall we go for a <Drink>
几个字符串是否有可能获得通过的标签C#正则表达式捕捉?像<Name>, <drink>
等? 我无法弄清楚。
说我有喜欢C#正则表达式提取标签
Hi my name is <Name>
Hi <name>, shall we go for a <Drink>
几个字符串是否有可能获得通过的标签C#正则表达式捕捉?像<Name>, <drink>
等? 我无法弄清楚。
肯定的:
Regex.Matches(myString, "<([^>]+)>");
PowerShell的例子:
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>
为什么不做更简单的事情就像使用C#s String.Replace一样?你传入要替换的字符串,并给它任何你想要替换的值。
在这里看到的例子:http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
感谢您的回复。 我不能这样做你建议,因为值在DB – SysAdmin 2010-03-27 19:37:53
"</?[a-z][a-z0-9]*[^<>]*>"
要使用它,尝试这样的事情:
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
}
嗨,谢谢你的工作.....我一直在尝试很多。*的中间。 – SysAdmin 2010-03-27 19:43:48