我正在尝试解析地址,以便可以检索地址,地址编号和地址字母。
string address = "Mushroom road 2b";
我正在尝试
var addressName = "Mushroom Road";
var addressNumber = "2";
var addressLetter = "b";
我尝试使用split并使用子字符串对其进行解析。
string adress = "Mushroom road 2b";
string temp = adress;
var res = temp.Substring(temp.IndexOf(' '));
//check if res first letter = number?
//Not sure how to use regex really
var addressNumber = Regex.Replace(temp, @"[^\d]", "");
RES将是2b路。但是我想先检索蘑菇道,然后检索2,然后检索b。
答案 0 :(得分:4)
您可以尝试正则表达式,例如:
using System.Text.RegularExpressions;
...
var match = Regex.Match(
source,
@"(?<street>[A-Z][A-Za-z'\s]*)(?<number>[0-9]+)\s*(?<letter>[a-zA-Z]*)");
if (match.Success) {
// "Mushroom Road"
var addressStreet = match.Groups["street"].Value.Trim();
// "2"
var addressNumber = match.Groups["number"].Value;
// "b"
var addressLetter = match.Groups["letter"].Value;
...
}
模式(?<street>[A-Z][A-Za-z'\s]*)(?<number>[0-9]+)\s*(?<letter>[a-zA-Z]*)
的意思是
(?<street>[A-Z][A-Za-z'\s]*) one capital letter A..Z followed by
zero or more letters (a..z A..Z),
apostrophes and whitespaces
(?<number>[0-9]+) one or more digits
\s* zero or more whitespaces
(?<letter>[a-zA-Z]*) zero or more letters a..z A..Z
演示:
string[] tests = new string[] {
"Mushroom road 2b",
"Mushroom road 2 b",
"Mushroom 2"
};
var results = tests
.Select(test => new {
test,
match = Regex.Match(
test,
@"(?<street>[A-Z][A-Za-z'\s]*)(?<number>[0-9]+)\s*(?<letter>[a-zA-Z]*)")
})
.Select(item => new {
test = item.test,
street = item.match.Groups["street"].Value.Trim(),
number = item.match.Groups["number"].Value,
letter = item.match.Groups["letter"].Value
})
.Select(item =>
$"{item.test,-20} => \"{item.street}\"; \"{item.number}\"; \"{item.letter}\"");
string report = string.Join(Environment.NewLine, results);
Console.Write(report);
结果:
Mushroom road 2b => "Mushroom road"; "2"; "b"
Mushroom road 2 b => "Mushroom road"; "2"; "b"
Mushroom 2 => "Mushroom"; "2"; ""
答案 1 :(得分:2)
这可能已经晚了,但这是我不使用Regex的解决方案。
string address = "Mushroom road 2b";
var helpvar = "";
// Getting the last character and saving it
helpvar = address.Remove(0, address.Length -1);
// Removing the last character
address = address.Remove(address.Length - 1);
// String interpolation output
// Adding the string without last character
// Adding the last character , because we want them sperated
Console.Write($"{address} {helpvar}");
答案 2 :(得分:-2)
Split方法可能有用。
var parts = address.Split(' ');
addressName = parts[0];
addressNumber = parts[1];
addressLetter = parts[2];
或将正则表达式与组配合使用。
误读。
您现在可以使用parts [2]并使用.Substring。
您还可以迭代所有字符并对其进行测试。 Char类有一些有用的方法。