简体   繁体   中英

Escape character in c#

I stuck in escape sequence in program.

I am getting input from xml file in single quotes.

So in string it may contain any escape sequence.

So what I want to do is consider next character after escape character as normal character.

Example: '\abc\\t'
 Output: 'abc\t'

So for that I created regular expression which is:

Regex.Replace(SearchString, @"\\?(?<Character>)", "${Character}");

But it replaces all escape character and gives output :

abct

Please help me how can I do it?

The named group in your regular expression matches nothing, so it does the same as Replace(@"\\", "") .

Match one character in the group:

SearchString = Regex.Replace(SearchString, @"\\(.)", "$1");

尝试这个:

Regex.Replace(SearchString, @"\\(?<Character>[^\\])", "${Character}");

Basically, you need parse your string character by character. Pseudo-code:

Loop through all characters c:
    If c is escape character:
        Read next character and do something special (\t is tab, \\ is backslash, ...)
    Else:
        Copy character to output

What if you do like:

SearchString = SearchString.Replace(@"\\", @"\");

It would give:

\abc\t

Here escaped \\t is now treated as a "tab character".

It is what you are looking for ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM