更换(解析" \"以非法方式结束) [英] Replace (parsing "\" illegal at end of pattern)
问题描述
好吧,我有C#一个小型程序,所以我这样做,我的程序
单词搜索=T:\ \
wordReplace =T:\\Gestion\\
含量= Regex.Replace(内容,单词搜索,wordReplace);
但不起作用。错误IR解析。T:\ - 非法\的模式结束
任何想法
<? p>谢谢!
对不起!也许我不解释好。所以......我再试试。
我做了一个表格以输入一个字符串,但如果这个字符串为T:\,该计划以T:\。因此,该字符串我保存在一个变量workShearch
在这个变量中使用:
内容= Regex.Replace(内容,Regex.Escape(单词搜索),Regex.Escape(wordReplace));
但是,这行包含一个错误,因为单词搜索在这种情况下,为T:\,和该方案trhow我像一个例外:
IR解析错误。T:\ - 非法\的模式结束
谢谢!
您应该逃避 \
的格局。要么使用T:\\\\
或逐字字符串(逐字字符串的优点是转义序列不处理,这使得它很容易编写):
VAR单词搜索= @T:\\;
VAR wordReplace = @T:\Gestion\
含量= Regex.Replace(内容,单词搜索,wordReplace);
Well I have a mini program with C#, so I do this in my program
wordSearch = "T:\\"
wordReplace = "T:\\Gestion\\"
content = Regex.Replace(content, wordSearch, wordReplace);
But doesn't work. The error ir parsing "T:\" - illegal \ at end of pattern.
Any idea ?
Thanks!
Sorry! Perhaps I don't explain well. So... I try again
Hi,
I did a form taking a string for input, but if this string is "T:\", the program take "T:\". So, this string I save in a variable "workShearch".
After this variable uses in:
content = Regex.Replace(content, Regex.Escape(wordSearch), Regex.Escape(wordReplace));
But this line contain an error, because wordSearch in this case is "T:\", and the program trhow me an exception like that:
The error ir parsing "T:\" - illegal \ at end of pattern.
Thanks!
You should escape \
in pattern. Either use "T:\\\\"
or verbatim string literal ( advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write):
var wordSearch = @"T:\\";
var wordReplace = @"T:\Gestion\";
content = Regex.Replace(content, wordSearch, wordReplace);
这篇关于更换(解析" \"以非法方式结束)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!