我遇到这样的正则表达式问题,
给出一个输入字符串,例如NAMEtony smithNUMBER100ADDRESSLAS vegas
输入字符串具有多个用NAME, NUMBER, ADDRESS
标记的字段,这些字段可以是可选的,并且顺序不固定。
(可以为ADDRESSLAS vegasNAMEtony smithNUMBER100, or NUMBER100NAMEtony smith
),
我正在尝试使用正则表达式
(NAME([A-Za-z0-9\s]{1,}))?(NUMBER(([A-Za-z0-9\s]{1,}))?(ADDRESS([A-Za-z0-9\s]{1,}))?
获取匹配项,但不起作用。有人可以帮忙吗?
答案 0 :(得分:0)
在您的正则表达式中,您似乎无法在数据中包含<
和>
。
它使您可以更轻松地解析数据:
var text = "NAMEtony smithNUMBER100ADDRESSLAS vegas";
var escapedText =
text.Replace("ADDRESS", "<ADDRESS>")
.Replace("NAME", "<NAME>")
.Replace("NUMBER", "<NUMBER>");
var dataFields = escapedText.Split('<'); // Now you have strings which look like "NAME>...."
foreach (var dataField in dataFields) {
if (dataField == "") // Somehow handle empty string in the beginning
continue;
var splitted = dataField.Split('>');
string field = splitted[0], data = splitted[1];
switch (field) {
case "NAME": ...
...
}
}
答案 1 :(得分:0)
这适用于输入字符串的所有三种情况。
优点:非常简单的正则表达式。
缺点:在if
语句中为变量手动设置值。
var input = "NAMEtony smithNUMBER100ADDRESSLAS vegas";
//var input = "ADDRESSLAS vegasNAMEtony smithNUMBER100";
//var input = "NUMBER100NAMEtony smith";
var pattern = "(?'field'NAME|NUMBER|ADDRESS)(?'value'.+?)(?=NAME|NUMBER|ADDRESS|$)";
var matches = Regex.Matches(input, pattern);
string name = null;
string number = null;
string address = null;
foreach (Match m in matches)
{
var field = m.Groups["field"].Value;
var value = m.Groups["value"].Value;
if (field == "NAME")
name = value;
else if (field == "NUMBER")
number = value;
else if (field == "ADDRESS")
address = value;
}
Console.WriteLine(name);
Console.WriteLine(number);
Console.WriteLine(address);