如果单词以%!开头,以!%
结尾,我需要从一段文字中提取一些单词我认为正则表达式对此有好处但不幸的是我的正则表达不是那么好......好吧它非常糟糕......好吧它不存在:(。
示例文字
You don't want no %!beef!%, boy
Know I run the streets, boy
Better follow me towards
Downtown
What you see is what you get %!girl!%
Don't ever forget girl
Ain't seen nothing yet until you're
%!Downtown!%
预期结果
beef, girl, Downtown
如何使用或不使用正则表达式在C#中实现此目的?
答案 0 :(得分:0)
像这样:
var reg = new Regex(@"%!(?<word>\w+)!%");
var inStr = @"You don't want no %!beef!%, boy
Know I run the streets, boy
Better follow me towards
Downtown
What you see is what you get %!girl!%
Don't ever forget girl
Ain't seen nothing yet until you're
%!Downtown!%";
var results = reg.Matches(inStr).Cast<Match>().Select(m => m.Groups["word"].Value);
这将为您提供匹配单词的列表。将它转换为以逗号分隔的字符串是我要留给你的练习..
另外,下次你应该做一些快速研究,你最终还是要学习简单的正则表达式。