我可以使用适当的正则表达式的一些帮助来解析下面的字符串到3个变量。有评论说// TODO:
的部分是我需要正则表达式帮助的地方。现在我刚刚分配了一个静态值,但需要用解析示例文本的真正正则表达式来替换它。谢谢!我需要一些文本解析帮助(正则表达式/ C#)
// This is what a sample text will look like.
var text = "Cashpay @username 55 This is a sample message";
// We need to parse the text into 3 variables.
// 1) username - the user the payment will go to.
// 2) amount - the amount the payment is for.
// 3) message - an optional message for the payment.
var username = "username"; // TODO: Get the username value from the text.
var amount = 55.00; // TODO: Get the amount from the text.
var message = "This is a sample message"; // TODO: Get the message from the text.
// now write out the variables
Console.WriteLine("username: " + username);
Console.WriteLine("amount: " + amount);
Console.WriteLine("message: " + message);
你甚至试过了吗?有很多用于构建Regexes的好工具,例如[Expresso](http://www.ultrapico.com/Expresso.htm)或[Regex Buddy](http://www.regexbuddy.com/)。 – Lucero 2011-05-01 16:36:37
我在正则表达式方面遇到了挑战:-)我一直在做一些像text.substring(0,text.indexOf('@'....)的东西,你甚至不想看到,我是寻找一些干净的表达,这些人真的很擅长这个东西 – 2011-05-01 16:41:49
我链接的工具可以帮助你建立表达式,通过使用它们,你应该没有问题做你需要做的解析这些简单的字符串,我更喜欢正则表达式“手动”解析,因为这允许您声明性地定义要匹配的模式,而不是强制搜索部分;从而使用正则表达式“免费”输入验证(如果模式不匹配,则输入无效) – Lucero 2011-05-01 16:45:59