正则表达式,替换问题

liuhui810 2009-03-12 12:26:39
source="my number is 999.99"
result="我的数字是999.99"
怎么替换,我的代码:


public void replace()
{
Regex regex = new Regex(@"my number is ([0-9\.]{3,9})");
string result = regex.Replace(@"my number is 999.99", "我的数字是$1");

textBox1.Text = result;
}

反正我得不出来我要得结果。求助。
...全文
112 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhui810 2009-03-12
  • 打赏
  • 举报
回复
楼上的两位都不对。
我姓区不姓区 2009-03-12
  • 打赏
  • 举报
回复
string result = source.Replace("my number is ","我的数字是");
jinjazz 2009-03-12
  • 打赏
  • 举报
回复
string result = System.Text.RegularExpressions.Regex.Replace(@"my number is 999.99", @"(-?\d*)\.?\d+", "我的数字是$0");
shiboss 2009-03-12
  • 打赏
  • 举报
回复
学习 帮顶
birdlonger 2009-03-12
  • 打赏
  • 举报
回复
string source = "my number is 999.99";
Regex thisregex = new Regex(@"[^0-9]+(?=[[0-9]+,[0-9]+\.[0-9]+])");
string result = thisregex.Replace(source, "我的数字是");
Console.WriteLine(result);
Console.ReadLine();
试下这个..刚测了下结果还可以.
femg93 2009-03-12
  • 打赏
  • 举报
回复
我们用的也是那种呀
dxpws 2009-03-12
  • 打赏
  • 举报
回复
luck
liuhui810 2009-03-12
  • 打赏
  • 举报
回复
怪了,我这回一试就通了。刚才老是又重复的部分。
见了鬼了。
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
那么,LZ的代码没有问题,能正确工作:

using System;
using System.Text.RegularExpressions;

class Program
{
static void Main()
{
Regex regex = new Regex(@"my number is ([0-9\.]{3,9})");
string result = regex.Replace(@"my number is 999.99", "我的数字是$1");
Console.WriteLine(result); // 輸出:我的数字是999.99
}
}
liuhui810 2009-03-12
  • 打赏
  • 举报
回复
哥哥,翻译句子不可能是英中文一对一的,数字的位置肯定要变的。

[Quote=引用 12 楼 wuyi8808 的回复:]
引用 11 楼 liuhui810 的回复:

我只是举个例子,我有很多翻译的句子需要转换,但数字不变,位置会变。



那就更没有必要用正则了:


C# codepublic void replace()
{
string source = "this is 123, that is 456.";
string result = source.Replace("this ", "这").Replace("that ", "那").Replace("is ", "是");
textBox1.Text = result;
}
[/Quote]
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
点“管理菜单” -> “生成帖子”就可刷新帖子了,用不着每次无必要的回复。

|
|
|
|
|
|
|
|
V
liuhui810 2009-03-12
  • 打赏
  • 举报
回复
看不见
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 liuhui810 的回复:]

我只是举个例子,我有很多翻译的句子需要转换,但数字不变,位置会变。

[/Quote]

那就更没有必要用正则了:

public void replace()
{
string source = "this is 123, that is 456.";
string result = source.Replace("this ", "这").Replace("that ", "那").Replace("is ", "是");
textBox1.Text = result;
}
liuhui810 2009-03-12
  • 打赏
  • 举报
回复

我只是举个例子,我有很多翻译的句子需要转换,但数字不变,位置会变。
[Quote=引用 7 楼 wuyi8808 的回复:]
根本没有必要用正则:

C# code public void replace()
{
string result = "my number is 999.99".Replace("my number is ", "我的数字是");
textBox1.Text = result;
}
[/Quote]
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/yd1hzczs.aspx

.NET Framework 开发人员指南
正则表达式选项

更新:2007 年 11 月

可以使用影响匹配行为的选项修改正则表达式模式。可以通过下列两种基本方法之一设置正则表达式选项:可以在 Regex (pattern, options) 构造函数中的 options 参数中指定,其中 options 是 RegexOptions 枚举值的按位“或”组合;也可以使用内联 (?imnsx-imnsx:) 分组构造或 (?imnsx-imnsx) 其他构造在正则表达式模式内设置它们。

在内联选项构造中,一个选项或一组选项前面的减号 (-) 用于关闭这些选项。例如,内联构造 (?ix-ms) 将打开 IgnoreCase 和 IgnorePatternWhiteSpace 选项而关闭 Multiline 和 Singleline 选项。默认情况下,关闭所有正则表达式选项。

下表列出了 RegexOptions 枚举的成员以及等效的内联选项字符。请注意,选项 RightToLeft 和 Compiled 只适用于表达式整体而不允许内联。(它们只能在 Regex 构造函数的 options 参数中指定。) 选项 None 和 ECMAScript 不允许内联。

IgnoreCase i 指定不区分大小写的匹配。
liuhui810 2009-03-12
  • 打赏
  • 举报
回复
刷新
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liuhui810 的回复:]
(?i)是什么意思啊?
[/Quote]

(?i) 是不区分大小写的匹配。
wuyi8808 2009-03-12
  • 打赏
  • 举报
回复
根本没有必要用正则:
        public void replace()
{
string result = "my number is 999.99".Replace("my number is ", "我的数字是");
textBox1.Text = result;
}
liuhui810 2009-03-12
  • 打赏
  • 举报
回复
(?i)是什么意思啊?
止戈而立 2009-03-12
  • 打赏
  • 举报
回复
string result = Regex.Replace(@"my number is 999.99", @"(?i)my\s+number\s+is\s+([0-9.]{3,9})", "我的数字是$1");

111,126

社区成员

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

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

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