美好的一天...... 我需要找到一种方法,用c#和regex表达式,首先,在字符串列表中找到一个单词,让我们称之为搜索。 字符串列表的示例:我有许多字符串列表列表;这只是一个包含4个字符串的列表:
{" port MegaModuleRefitLogEventMegaModuleRefitLogEvent.REFIT_FINISHED,moduleId,id,ActionJSON.encodeJSONanalyticExport,ActionJSON.encodeJSON_pendingDesign.analyticExport,ActionJSON.port.encodeJSON_pendingDesign.analyticExportthis;",
"private static var _warReportManager:WarReportManager = new WarReportManager.port;",
"import com.waterworld.managers.warreport.WarReportManager;",
"else iflastBattleReport.report != port && lastBattleReport.port"}
现在,我的搜索是“端口” 首先,我用过:
string search="port"
if (myStrList.Any(str => str.Contains(search)))
{
//go to the funtion that does the actual replacing etc
}
找到它......那么,我的表达式找到包含搜索的所有列表......包括无效的场景......
这个给定的列表在示例中包含4个字符串...所有5个以不同的形式出现了多个“port”。 我的字符串列表有时会出现很多次,有时则没有...但它是一个列表。 我需要用上面的表达式找到可接受的事件: 在列表的第一个字符串:
“port”在开头没有空格是有效的;
“端口”。没有“。”已验证...”。”可以是。“,:'[] {}<>;
在列表的第二个字符串上: - - “。港口;”在没有“。”的情况下有效。和“;”
在第三个字符串上:
在第四个字符串上:
然后一旦我输入包含搜索的有效字符串... 我需要用它们做一些功能。其中一个用“myport”代替给定的单词 note..all搜索//替换是区分大小写的。
string pattern = @"\b"+search+@"\b";
string replace = "myport";
string result = Regex.Replace(input, pattern, replace);
那么,这只是那些边界是白色空间的......而不是其他场景......
任何帮助都将受到重视。
我环顾四周,这些整个单词场景中的大部分都是特定于空白区域的,所以......在这里可能是我的情况的答案......
问候
EDIT。 感谢@jdweng
我的搜索结果如下:
string pattern = string.Format(@"[\s]+{0}[\s.!?$]+", search);
Regex matcher = new Regex(pattern);
if (teslist.Any(str => matcher.IsMatch(str)))
{
Console.WriteLine("string match found: "");
}
编辑2 ... jdweng的答案很接近......不是必需的结果...... 香港专业教育学院试图将要求如下: 搜索模式:
jdweng的回答确实包含了所有的选项...并且......它取代了搜索,包括那些开始或结束字符,其中,我只需要替换给定的搜索字符串。
答案 0 :(得分:0)
以下代码有效。我替换了\ b以获得任何空白区域。并加上带有加号的方括号以获得多次出现。最后,在句子结束时可能会出现类似且包含的句号,感叹号和问号:
sess = requests.Session()
sess.config['keep_alive'] = False
答案 1 :(得分:0)
好的..首先,过滤掉可能的匹配列表...以查看列表中是否匹配...我做了:
string pattern = string.Format(@"[\W\s\b]{0}[\b\s\W]", search);
Regex matcher = new Regex(pattern);
if (teslist.Any(sTrr => matcher.IsMatch(sTrr)))
{
Console.WriteLine("search found for list");
//....do stuff
}
然后......在替换部分..我必须在模式上创建正则表达式匹配列表,并分别替换每个匹配...
string pattern = string.Format(@"[\W\s\b]{0}[\b\s\W]", search);
Regex ItemRegex = new Regex(pattern, RegexOptions.Compiled);
List<KeyValuePair<string,string>> MatchList = new List<KeyValuePair<string,string>>();
foreach (Match ItemMatch in ItemRegex.Matches(str))
{
string myMatch = ItemMatch.ToString();
string myReplace = (ItemMatch.ToString().Replace(read, replace));
KeyValuePair<string, string> ThisPair = new KeyValuePair<string, string>(myMatch, myReplace);
MatchList.Add(ThisPair);
}
string newValue = str;
foreach (KeyValuePair<string,string> KVstr in MatchList)
{
newValue = (str.Replace(KVstr.Key, KVstr.Value));
}
Console.WriteLine("str: " + str);
Console.WriteLine(" newValue: " + newValue);