C#正则表达式提取标记
比如说我有几根绳子C#正则表达式提取标记,c#,.net,regex,C#,.net,Regex,比如说我有几根绳子 Hi my name is <Name> Hi <name>, shall we go for a <Drink> 你好,我的名字是 嗨,我们去喝一杯好吗 是否可以通过c#Regex获取标签?比如,等等? 我无法正确使用它。为什么不做一些更简单的事情,比如使用C#s字符串。替换?您传入要替换的字符串,并为其指定任何要替换的值 请参见此处的示例:为什么不做一些更简单的事情,比如使用C#s String.Replace?您传入要替换的字符串
Hi my name is <Name>
Hi <name>, shall we go for a <Drink>
你好,我的名字是
嗨,我们去喝一杯好吗
是否可以通过c#Regex获取标签?比如,
等等?
我无法正确使用它。为什么不做一些更简单的事情,比如使用C#s字符串。替换?您传入要替换的字符串,并为其指定任何要替换的值
请参见此处的示例:为什么不做一些更简单的事情,比如使用C#s String.Replace?您传入要替换的字符串,并为其指定任何要替换的值 有关示例,请参见此处:当然:
Regex.Matches(myString, "<([^>]+)>");
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>
PS>$s=@'
>>嗨,我的名字是
>>嗨,我们去喝一杯好吗
>> '@
>>
PS>[regex]::匹配($s,']+)>')|英尺
组成功捕获索引长度值
------ ------- -------- ----- ------ -----
{,Name}True{}14 6
{,name}True{}25 6
{,喝酒}真的{}517
当然可以:
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>
PS>$s=@'
>>嗨,我的名字是
>>嗨,我们去喝一杯好吗
>> '@
>>
PS>[regex]::匹配($s,']+)>')|英尺
组成功捕获索引长度值
------ ------- -------- ----- ------ -----
{,Name}True{}14 6
{,name}True{}25 6
{,喝酒}真的{}517
“”
要使用它,请尝试以下操作:
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
}
试试看
{
Regex regexObj=new Regex(“,RegexOptions.IgnoreCase);
Match matchResults=regexObj.Match(subjectString);
while(matchResults.Success)
{
//做事
//匹配文本:匹配结果。值
//匹配开始:匹配结果。索引
//匹配长度:匹配结果。长度
matchResults=matchResults.NextMatch();
}
}
捕获(参数异常)
{
//正则表达式中的语法错误
}
“”
要使用它,请尝试以下操作:
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
}
试试看
{
Regex regexObj=new Regex(“,RegexOptions.IgnoreCase);
Match matchResults=regexObj.Match(subjectString);
while(matchResults.Success)
{
//做事
//匹配文本:匹配结果。值
//匹配开始:匹配结果。索引
//匹配长度:匹配结果。长度
matchResults=matchResults.NextMatch();
}
}
捕获(参数异常)
{
//正则表达式中的语法错误
}
感谢您的回复。我不能像你建议的那样做,因为这些值都在数据库中。谢谢你的回答。我不能像你建议的那样做,因为这些值是在一个DB中