我实际使用这个脚本:
using (SqlDataReader reader_org = select_org.ExecuteReader())
{
while (reader_org.Read())
{
if (reader_org.IsDBNull(1) | reader_org.IsDBNull(0))
continue;
int cislo = reader_org.GetInt32(0);
string s = reader_org.GetString(1);
string ulice;
Match m = Regex.Match(s, @"(\d+)");
string cp = m.Groups[0].Value;
if (cp.Length > 0)
{
s = s.Replace(cp, "").Trim();
int number = Convert.ToInt32(cp);
}
if (s.Contains('/'))
{
Match l = Regex.Match(s, @"(\d+)");
string co = l.Groups[0].Value;
if (co.Length > 0)
{
s = s.Replace(co, "").Trim();
int number = Convert.ToInt32(co);
}
s = s.Replace('/', ' ').Trim();
Definitions.co.Add(co);
MessageBox.Show("CO: " + co);
}
ulice = s;
Definitions.Subjekt.Add(cislo);
Definitions.Ulice.Add(ulice);
Definitions.cp.Add(cp);
MessageBox.Show("Adresa " + ulice + " " + cp);
}
}
我的字符串在以下内容中获取此数据:(完整地址)
// I need every value separately
Complete address - > Streets House number OC
5 renvan 5 /13 5 renvan 5 13
5 renwan 13 5 renwan 13 0
Terak 516 Terak 516 0
Terak 516/87 Terak 516 87
Timbron 5 87 /69 Timbron 5 87 69
但现在我得到第一个数字int文本,但如果我将从正确的网站读取所以我的问题将得到解决,你能帮助我,我该如何解决这个问题?
答案 0 :(得分:0)
我写了一些代码,如果我理解正确,它可以解决你的问题。如果没有,请根据您的需要进行调整:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Test
{
public static void Main()
{
var input = new List<string>() {
@"5 renvan 5 /13",
@"5 renwan 13",
@"Terak 516 ",
@"Terak 516/87",
"Timbron 5 87 /69"};
foreach (var item in input)
{
var workItem = item.Trim().Replace("/", " ").Split(' ').Where(i => "" != i.Trim()).ToList();
if (workItem.Count() > 1)
{
string firstRightDigit = workItem[workItem.Count() - 1];
string secondRightDigit = workItem[workItem.Count() - 2];
int CO = 0;
if (!Int32.TryParse(secondRightDigit, out CO))
{
secondRightDigit = firstRightDigit;
firstRightDigit = "0";
}
Console.WriteLine(string.Format("House number: {0} \t\t CO:{1}", secondRightDigit, firstRightDigit));
}
}
}
}
}
** ADDED **
我来到我的工作场所并创建另一个解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var input = new List<string>() {
@"5 renvan 5 /13",
@"5 renwan 13",
@"Terak 516 ",
@"Terak 516/87 ",
@"Timbron 5 827 /619",
@"Timbron 5 980 / 69 ",
@"Timbron 5 187 / 121"};
input.ForEach(item => {
Match match = Regex.Match(item, @"(\d+)(\s*)?/?(\s*\d+\s*)?$", RegexOptions.IgnoreCase);
Console.WriteLine(string.Format("Input string: {0,20}\tResult House #: {1,5}\tCO:{2,5}",
item,
match.Groups[1].Value.Trim(),
string.IsNullOrEmpty(match.Groups[3].Value.Trim()) ? "0" : match.Groups[3].Value.Trim()));
});
}
}
}