街道名称的地址信息

时间:2014-10-02 11:50:14

标签: c# regex

我需要什么:

输入:

Somestreet
Somestreet 12    
Somestreet 12 A    
Somestreet 12-14

输出:

Somestreet

Somestreet | 12

Somestreet | 12 | A 

Somestreet | 12 | - | 14

其中|是分隔符

我做了什么:

var pattern = @"(\d+)";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);

var matchCollection = regex.Split(input);

var street = matchCollection[0];

if (matchCollection.Length > 1)
{
   houseNumber = matchCollection[1];
}

if (matchCollection.Length > 2)
{
   houseNumberLetter = matchCollection[2];
}

前三种情况可以,但不是第四种情况。

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

这有用吗?

string result = string.Join(" | ", Regex.Matches(input, @"(\w+|\-)").Cast<Match>().Select(d => d.Value));

<强>更新

更好?

string result = string.Join(" | ", Regex.Matches(input, @"([a-zA-Z\. ]+|[0-9]+|\-)").Cast<Match>().Select(d => d.Value.Trim()));

我必须将字母和数字匹配分开以包含可能的空格。

相关问题