简体   繁体   English

我需要一些文本解析帮助(正则表达式/C#)

[英]I need some Text Parsing Help (Regular Expressions / C#)

I could use a little help with proper regular expressions to parse the following string into 3 variables.我可以使用一些正确的正则表达式帮助将以下字符串解析为 3 个变量。 The part with comments saying // TODO: is where I need help with the regular expressions.带有注释的部分// TODO:是我需要正则表达式帮助的地方。 For now I have just assigned a static value, but need to replace that with real regular expressions that parse the sample text.现在我刚刚分配了一个 static 值,但需要用解析示例文本的真实正则表达式替换它。 Thanks!谢谢!

// 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);

You can use capturing groups:您可以使用捕获组:

var regex = new Regex(@"^Cashpay\s+@([A-Za-z0-9_-]+)\s+(\d+)\s+(.+)$");
var text = "Cashpay @username 55 This is a sample message";

var match = regex.Match(text);

if (!match.Success)
    //Bad string! Waaaah!

string username = match.Groups[1].Value;
int amount = int.Parse(match.Groups[2].Value);
string message = match.Groups[3].Value;

This method does not do input validation;此方法不进行输入验证; in some cases this might be ok (eg the input is coming from a source which has already been validated).在某些情况下,这可能没问题(例如,输入来自已验证的源)。 If you are getting this from user input you should probably use a method that is more robust.如果您从用户输入中获取此信息,您可能应该使用更强大的方法。 If it is coming from a trusted source but has multiple formats (eg "Cashpay" is one of many choices) you could use a switch or if statement for flow control after the split:如果它来自受信任的来源但有多种格式(例如“Cashpay”是众多选择之一),您可以在拆分后使用 switch 或 if 语句进行流量控制:

// make sure you validate input (coming from trusted source?) 
// before you parse like this.

string list[] = text.Split(new char [] {' '});

if (list[0] == "Cashpay")
{
    var username = list[1].SubString(1);
    var amount = list[2];
    var message = string.Join(' ',list.Skip(3));
}

or或者

// make sure you validate input (coming from trusted source?) 
// before you parse like this.

string list[] = text.Split(new char [] {' '},4);

if (list[0] == "Cashpay")
{
    var username = list[1].SubString(1);
    var amount = list[2];
    var message = list[3];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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