C#怎么处理特殊符号?和$.....求解....

liyifei12345 2013-07-12 10:37:52
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;


StreamReader sr = new StreamReader("d:\test.txt", Encoding.Default);
StringBuilder sb = new StringBuilder(sr.ReadToEnd());
sr.Close();
StreamWriter sw;
sw = new StreamWriter("d:\test.txt", false, Encoding.UTF8);
string resultsb = sb.ToString();
string resultsbw = "";
resultsbw = Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase);
sw.Write(resultsbw.ToString());
sw.Close();
this.Text = f;
Thread.Sleep(1);



test.txt文本文件里面的内容为"test.asp?id=$id$"
text1.Text我设的为asp?id=$id$
text2.Text我设的为xxxasp?id=$id$xxx

可结果不是不替换就是替换的时候加几次原来内容,后来查了查是特殊符号?和$符号的原因,但是我不知道该如何解决。麻烦来个高手教我怎么处理一下。拜托了。

...全文
1308 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
100327870 2013-07-14
  • 打赏
  • 举报
回复
像高手学习、。、、
hudsonhuang 2013-07-13
  • 打赏
  • 举报
回复
引用 11 楼 caozhy 的回复:
[quote=引用 9 楼 liyifei12345 的回复:] [quote=引用 4 楼 caozhy 的回复:] [quote=引用 2 楼 liyifei12345 的回复:] [quote=引用 1 楼 caozhy 的回复:] \$ 就可以了。$有特殊含义,表示文本结束。
加上\$ 是这样吗?asp\?id=\$id\$ ?和$符号前面都加上\[/quote] 等号也要转义。[/quote] Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); txt1.Text如何加@啊。基础不好。麻烦啦。[/quote] 你可以忽略那个人说的话了,谢谢。[/quote] 版主麻烦有时间帮我看看MONO那个帖子的问题!!!!!
hudsonhuang 2013-07-13
  • 打赏
  • 举报
回复
-_- 由于楼主说忽略我说话,所以我怒了,哈哈 终于认真看完楼主的题目,很疑惑为什么要用正则表达式来替换呢? 直接用string.replace就好了。。。。。
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 11 楼 caozhy 的回复:
[quote=引用 9 楼 liyifei12345 的回复:] [quote=引用 4 楼 caozhy 的回复:] [quote=引用 2 楼 liyifei12345 的回复:] [quote=引用 1 楼 caozhy 的回复:] \$ 就可以了。$有特殊含义,表示文本结束。
加上\$ 是这样吗?asp\?id=\$id\$ ?和$符号前面都加上\[/quote] 等号也要转义。[/quote] Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); txt1.Text如何加@啊。基础不好。麻烦啦。[/quote] 你可以忽略那个人说的话了,谢谢。[/quote]
引用 10 楼 dogfish 的回复:
@好像不是特殊字符吧?
那有没有办法不加\进行转义。这要是字符多的话我岂不得全部都得加\一个一个的加多累了。。
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 10 楼 dogfish 的回复:
@好像不是特殊字符吧?
那有没有办法不加\进行转义。这要是字符多的话我岂不得全部都得加\一个一个的加多累了。。
threenewbee 2013-07-13
  • 打赏
  • 举报
回复
引用 9 楼 liyifei12345 的回复:
[quote=引用 4 楼 caozhy 的回复:] [quote=引用 2 楼 liyifei12345 的回复:] [quote=引用 1 楼 caozhy 的回复:] \$ 就可以了。$有特殊含义,表示文本结束。
加上\$ 是这样吗?asp\?id=\$id\$ ?和$符号前面都加上\[/quote] 等号也要转义。[/quote] Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); txt1.Text如何加@啊。基础不好。麻烦啦。[/quote] 你可以忽略那个人说的话了,谢谢。
Dogfish 2013-07-13
  • 打赏
  • 举报
回复
@好像不是特殊字符吧?
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 4 楼 caozhy 的回复:
[quote=引用 2 楼 liyifei12345 的回复:] [quote=引用 1 楼 caozhy 的回复:] \$ 就可以了。$有特殊含义,表示文本结束。
加上\$ 是这样吗?asp\?id=\$id\$ ?和$符号前面都加上\[/quote] 等号也要转义。[/quote] Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); txt1.Text如何加@啊。基础不好。麻烦啦。
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 3 楼 hudsonhuang 的回复:
@“” 这样试试吧
Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); txt1.Text如何加@啊。基础不好。麻烦啦。
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 6 楼 hudsonhuang 的回复:
[quote=引用 5 楼 liyifei12345 的回复:] [quote=引用 3 楼 hudsonhuang 的回复:] @“” 这样试试吧
老大加不了@读的是文本,[/quote] 写这么一大段代码我都懒得看,把问题描述清楚也不难吧。。。 还可以用xml那种啥格式的转换,如:& -> &[/quote]不懂。
hudsonhuang 2013-07-13
  • 打赏
  • 举报
回复
引用 5 楼 liyifei12345 的回复:
[quote=引用 3 楼 hudsonhuang 的回复:] @“” 这样试试吧
老大加不了@读的是文本,[/quote] 写这么一大段代码我都懒得看,把问题描述清楚也不难吧。。。 还可以用xml那种啥格式的转换,如:& -> &
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 3 楼 hudsonhuang 的回复:
@“” 这样试试吧
老大加不了@读的是文本,
threenewbee 2013-07-13
  • 打赏
  • 举报
回复
引用 2 楼 liyifei12345 的回复:
[quote=引用 1 楼 caozhy 的回复:] \$ 就可以了。$有特殊含义,表示文本结束。
加上\$ 是这样吗?asp\?id=\$id\$ ?和$符号前面都加上\[/quote] 等号也要转义。
hudsonhuang 2013-07-13
  • 打赏
  • 举报
回复
@“” 这样试试吧
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
求一解决方案
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 20 楼 hudsonhuang 的回复:
[quote=引用 18 楼 liyifei12345 的回复:] [quote=引用 17 楼 hudsonhuang 的回复:] [quote=引用 16 楼 liyifei12345 的回复:] [quote=引用 14 楼 hudsonhuang 的回复:] -_- 由于楼主说忽略我说话,所以我怒了,哈哈 终于认真看完楼主的题目,很疑惑为什么要用正则表达式来替换呢? 直接用string.replace就好了。。。。。
那是版主说的老大,不要冤枉我呀。。。string.replace肯定是没有问题。关键是正则这里不知道怎么处理。[/quote] 脑子想着版主,手上却打了楼主,呵呵 麻烦你先在网上找找《正则表达式30分钟入门》这个文章来看看!!!(我当年看了最少用一个上午) 这样的基础横冲直撞,大家说了也白说啊。。。。。。[/quote] 别啊,这个我日后再看。我现在就是想知道 text1.Text="asp?id=$id$" 下面这一句该怎么写? Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); 写成这样可以吗?Regex.Replace(resultsb, @(txt1.Text), txt2.Text, RegexOptions.IgnoreCase); 还是该怎写?text1.Text="asp?id=$id$"不能变,因为是读的控件里面的字符串。就是正则该怎么写你告诉我就行了。[/quote] 人家版主都叫你忽略我上面的话了。。。。 你自己不去看看一些入门的东西真的没法教 要用正则表达式的话,必须改的是匹配项,貌似就是第一个 -_-[/quote]我试了一下。好像版主说得对着呢。@就是不行。哈哈,睡觉去了。明天再说。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Text; using System.IO; using System.Threading; using System.Text.RegularExpressions; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { StreamReader sr = new StreamReader("d:\\test.txt", Encoding.Default); StringBuilder sb = new StringBuilder(sr.ReadToEnd()); sr.Close(); StreamWriter sw; sw = new StreamWriter("d:\\test.txt", false, Encoding.UTF8); string resultsb = sb.ToString(); string resultsbw = ""; //string t1 = "asp\\?id\\=\\$id\\$"; string t1 = @"asp?id=$id$"; string t2 = "xxxasp?id=$id$xxx"; resultsbw = Regex.Replace(resultsb, t1, t2, RegexOptions.IgnoreCase); sw.Write(resultsbw.ToString()); sw.Close(); //this.Text = f; Thread.Sleep(1); Console.WriteLine(resultsbw); } } } @的就没有执行。//的就执行替换了。。
hudsonhuang 2013-07-13
  • 打赏
  • 举报
回复
引用 18 楼 liyifei12345 的回复:
[quote=引用 17 楼 hudsonhuang 的回复:] [quote=引用 16 楼 liyifei12345 的回复:] [quote=引用 14 楼 hudsonhuang 的回复:] -_- 由于楼主说忽略我说话,所以我怒了,哈哈 终于认真看完楼主的题目,很疑惑为什么要用正则表达式来替换呢? 直接用string.replace就好了。。。。。
那是版主说的老大,不要冤枉我呀。。。string.replace肯定是没有问题。关键是正则这里不知道怎么处理。[/quote] 脑子想着版主,手上却打了楼主,呵呵 麻烦你先在网上找找《正则表达式30分钟入门》这个文章来看看!!!(我当年看了最少用一个上午) 这样的基础横冲直撞,大家说了也白说啊。。。。。。[/quote] 别啊,这个我日后再看。我现在就是想知道 text1.Text="asp?id=$id$" 下面这一句该怎么写? Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); 写成这样可以吗?Regex.Replace(resultsb, @(txt1.Text), txt2.Text, RegexOptions.IgnoreCase); 还是该怎写?text1.Text="asp?id=$id$"不能变,因为是读的控件里面的字符串。就是正则该怎么写你告诉我就行了。[/quote] 人家版主都叫你忽略我上面的话了。。。。 你自己不去看看一些入门的东西真的没法教 要用正则表达式的话,必须改的是匹配项,貌似就是第一个 -_-
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 17 楼 hudsonhuang 的回复:
[quote=引用 16 楼 liyifei12345 的回复:] [quote=引用 14 楼 hudsonhuang 的回复:] -_-
Regex.Replace(resultsb, @“+txt1.Text+”, txt2.Text, RegexOptions.IgnoreCase); 是不是这样写啊?
liyifei12345 2013-07-13
  • 打赏
  • 举报
回复
引用 17 楼 hudsonhuang 的回复:
[quote=引用 16 楼 liyifei12345 的回复:] [quote=引用 14 楼 hudsonhuang 的回复:] -_- 由于楼主说忽略我说话,所以我怒了,哈哈 终于认真看完楼主的题目,很疑惑为什么要用正则表达式来替换呢? 直接用string.replace就好了。。。。。
那是版主说的老大,不要冤枉我呀。。。string.replace肯定是没有问题。关键是正则这里不知道怎么处理。[/quote] 脑子想着版主,手上却打了楼主,呵呵 麻烦你先在网上找找《正则表达式30分钟入门》这个文章来看看!!!(我当年看了最少用一个上午) 这样的基础横冲直撞,大家说了也白说啊。。。。。。[/quote] 别啊,这个我日后再看。我现在就是想知道 text1.Text="asp?id=$id$" 下面这一句该怎么写? Regex.Replace(resultsb, txt1.Text, txt2.Text, RegexOptions.IgnoreCase); 写成这样可以吗?Regex.Replace(resultsb, @(txt1.Text), txt2.Text, RegexOptions.IgnoreCase); 还是该怎写?text1.Text="asp?id=$id$"不能变,因为是读的控件里面的字符串。就是正则该怎么写你告诉我就行了。
hudsonhuang 2013-07-13
  • 打赏
  • 举报
回复
引用 16 楼 liyifei12345 的回复:
[quote=引用 14 楼 hudsonhuang 的回复:] -_- 由于楼主说忽略我说话,所以我怒了,哈哈 终于认真看完楼主的题目,很疑惑为什么要用正则表达式来替换呢? 直接用string.replace就好了。。。。。
那是版主说的老大,不要冤枉我呀。。。string.replace肯定是没有问题。关键是正则这里不知道怎么处理。[/quote] 脑子想着版主,手上却打了楼主,呵呵 麻烦你先在网上找找《正则表达式30分钟入门》这个文章来看看!!!(我当年看了最少用一个上午) 这样的基础横冲直撞,大家说了也白说啊。。。。。。
加载更多回复(3)

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧