需要一些帮助,使用正则表达式从字符串中获取多个值。我可以从字符串中获取单个值,但不能获取多个值。
我有这个字符串:
[message:USERPIN]Message to send to the user
我需要提取 USERPIN 和消息。我知道如何获取密码:
Match sendMessage = Regex.Match(message, "\\[message:[A-Z1-9]{5}\\]");
只是不确定如何同时获取这两个值。
感谢您的帮助。
最佳答案
使用命名组以方便访问:
Match sendMessage = Regex.Match(message,
@"\[message:(?<userpin>[A-Z1-9]{5})\](?<message>.+)");
string pin = sendMessage.Groups["userpin"].Value;
string message = sendMessage.Groups["message"].Value;
关于C# 正则表达式帮助获取多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3993058/