[英]Replace string if starts with string in List
I have a string that looks like this 我有一个看起来像这样的字符串
s = "<Hello it´s me, <Hi how are you <hay"
and a List List<string> ValidList= {Hello, hay}
I need the result string to be like 和列表
List<string> ValidList= {Hello, hay}
我需要结果字符串像
string result = "<Hello it´s me, ?Hi how are you <hay"
So the result string will if it starts with an < and the rest bellogs to the list, keep it, otherwise if starts with < but doesn´t bellong to list replaces the H by ? 因此,如果结果字符串以<开头,其余的字符串将保留到列表中,则将其保留,否则,如果结果以<开头但不是在列表中则将H替换为?
I tried using the IndexOf to find the position of the < and the if the string after starsWith any of the strings in the List leave it. 我尝试使用IndexOf来找到<的位置,以及如果starsWithList中的任何字符串后的字符串都离开它,则查找位置。
foreach (var vl in ValidList)
{
int nextLt = 0;
while ((nextLt = strAux.IndexOf('<', nextLt)) != -1)
{
//is element, leave it
if (!(strAux.Substring(nextLt + 1).StartsWith(vl)))
{
//its not, replace
strAux = string.Format(@"{0}?{1}", strAux.Substring(0, nextLt), strAux.Substring(nextLt + 1, strAux.Length - (nextLt + 1)));
}
nextLt++;
}
}
To give the solution I gave as a comment its proper answer: 为了给出解决方案,我以评论的形式给出了正确的答案:
Regex.Replace(s, string.Format("<(?!{0})", string.Join("|", ValidList)), "?")
This (obviously) uses regular expressions to replace the unwanted <
characters by ?
这(显然)使用正则表达式将不需要的
<
字符替换为?
. 。 In order to recognize those characters, we use a negative lookahead expression.
为了识别这些字符,我们使用否定的超前表达。 For the example word list, this would look like this:
(?!Hallo|hay)
. 对于示例单词列表,它看起来像这样:(
(?!Hallo|hay)
。 This will essentially match only if what we are matching is not followed by Hallo
or hay
. 仅当我们匹配的对象后面没有
Hallo
或hay
这才基本匹配。 In this case, we are matching <
so the full expression becomes <(?!Hallo|hay)
. 在这种情况下,我们匹配
<
因此完整表达式变为<(?!Hallo|hay)
。
Now we just need to account for the dynamic ValidList
by creating the regular expression on the fly. 现在,我们只需要通过动态创建正则表达式来说明动态
ValidList
。 We use string.Format
and string.Join
there. 我们使用
string.Format
和string.Join
。
A possible solution using LINQ.It splits the string using <
and checks if the "word" (text until a blank space found) following is in the Valid List,adding <
or ?
可能的使用LINQ的解决方案。它将使用
<
拆分字符串,并检查后面的“单词”(文本直到找到空白)是否在有效列表中,并添加<
或?
accordingly. 因此。 Finally,it joins it all:
最后,它将所有内容结合在一起:
List<string> ValidList = new List<string>{ "Hello", "hay" };
string str = "<Hello it´s me, <Hi how are you <hay";
var res = String.Join("",str.Split(new char[] { '<' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => ValidList.Contains(x.Split(' ').First()) ? "<" + x : "?"+x));
Something like this without using RegEx or LINQ 不使用RegEx或LINQ的情况
string s = "<Hello it´s me, <Hi how are you <hay";
List<string> ValidList = new List<string>() { "Hello", "hay" };
var arr = s.Split(new[] { '<' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arr.Length; i++)
{
bool flag = false;
foreach (var item in ValidList)
{
if (arr[i].Contains(item))
{
flag = false;
break;
}
else
{
flag = (flag) ? flag : !flag;
}
}
if (flag)
arr[i] = "?" + arr[i];
else
arr[i] = "<" + arr[i];
}
Console.WriteLine(string.Concat(arr));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.