如何匹配正则表达式模式并从中提取数据
本文关键字:提取 数据 何匹配 正则表达式 模式 | 更新日期: 2023-09-27 17:57:55
在{key-value}Some text{/key}
、格式的文本区域中,我可以有0个或多个子字符串
例如This is my {link-123}test{/link} text area
我想遍历与此模式匹配的任何项,根据键和值执行和操作,然后用新字符串(由基于键的操作检索的锚链接)替换此子字符串。
我将如何在C#中实现这一点?
如果这些标签没有嵌套,那么您只需要在文件上迭代一次;如果嵌套是可能的,那么您需要为每个嵌套级别执行一次迭代。
这个答案假设大括号只作为标记分隔符出现(而不是,例如,在注释中):
result = Regex.Replace(subject,
@"'{ # opening brace
(?<key>'w+) # Match the key (alnum), capture into the group 'key'
- # dash
(?<value>'w+) # Match the value (alnum), capture it as above
'} # closing brace
(?<content> # Match and capture into the group 'content':
(?: # Match...
(?!'{/?'k<key>) # (unless there's an opening or closing tag
. # of the same name right here) any character
)* # any number of times
) # End of capturing group
'{/'k<key>'} # Match the closing tag.",
new MatchEvaluator(ComputeReplacement), RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
public String ComputeReplacement(Match m) {
// You can vary the replacement text for each match on-the-fly
// m.Groups["key"].Value will contain the key
// m.Groups["value"].Value will contain the value of the match
// m.Groups["value"].Value will contain the content between the tags
return ""; // change this to return the string you generated here
}
类似的东西?
Regex.Replace(text,
"[{](?<key>[^-]+)-(?<value>[^}])[}](?<content>.*?)[{][/]'k<key>[}]",
match => {
var key = match.Groups["key"].Value;
var value= match.Groups["value"].Value;
var content = match.Groups["content"].Value;
return string.format("The content of {0}-{1} is {2}", key, value, content);
});
使用.net正则表达式库。下面是一个使用Matches方法的示例:
http://www.dotnetperls.com/regex-matches
要替换文本,请考虑使用模板引擎,如Antlr
http://www.antlr.org/wiki/display/ANTLR3/Antlr+3+CSharp+目标
以下是Matches博客的示例
使用系统;使用System。文本RegularExpressions;
class Program
{
static void Main()
{
// Input string.
const string value = @"said shed see spear spread super";
// Get a collection of matches.
MatchCollection matches = Regex.Matches(value, @"s'w+d");
// Use foreach loop.
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine("Index={0}, Value={1}", capture.Index, capture.Value);
}
}
}
}
有关C#正则表达式语法的更多信息,您可以使用以下备忘单:
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet