Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自街道名称的地址信息_C#_Regex - Fatal编程技术网

C# 来自街道名称的地址信息

C# 来自街道名称的地址信息,c#,regex,C#,Regex,我需要什么: 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 (mat

我需要什么:

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];
}
对于输入:

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];
}
前三种情况可以,但第四种情况不行

你能帮我吗?

这行吗

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

更好

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

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

字符串之间是否要换行?字母
A
后面有空格吗?@AvinashRaj我想把信息分成许多部分。1.对于streetname,2。门牌号是3。带额外字母的门牌号(如有)。但是如果以“Streetname 12-14”的形式输入,我只读取Streetname 12和-@AvinashRaj,字母A后面可以有空格,但这不会是问题,因为我在使用
拆分后检查字符串!string.IsNullOrWhiteSpace
谢谢(\w+\124; \-)它工作得很好。如果输入是Some.street,则仅有两个问题是“.”。我怎么能忽略要点?第二个是数字前的空格。例:某街34号。谢谢!我将在下周尝试,如果是解决方案,我将接受它!:)