【问题标题】:C# Read text from right sideC#从右侧读取文本
【发布时间】:2013-10-26 07:56:52
【问题描述】:

我实际上使用了这个脚本:

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);
    }
}

我的字符串在while中获取这个数据:(完整地址)

                                                  // 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 文本,但是如果我从正确的站点读取,那么我的问题将得到解决,你能帮我解决这个问题吗?

【问题讨论】:

  • 我不明白你想要完成什么。我不确定从右到左阅读可以解决问题,您只需要创建正确的正则表达式并立即获取所需的一切。但同样,我只是不明白问题。
  • 好吧。完整地址是我在脚本中的字符串“s”中获得的值。但是我的脚本现在从文本中捕获第一个数字,如果文本包含这个字符(/),所以我重复选择数字的函数。但我只需要获得门牌号码和 oc,但 streetName contaions 太数字,但它不是门牌号码,但我的脚本现在也选择了,但我不需要它。现在你明白我说的了么?但是,如果我从右侧读取,那么我的脚本将起作用,因为右侧的第一个数字对于门牌号或 contaions (/) 是正确的,所以我获得了我需要的两个数值。

标签: c# regex replace substring right-to-left


【解决方案1】:

我写了一些代码,如果我理解正确,它可以解决你的问题。如果没有,请根据您的需要进行调整:

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));
                }

            }
        }
    }
}

** 已添加 **

我到达了我的工作场所并创建了另一个解决方案:

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()));
            });

        }

    }
}

【讨论】:

  • 你能添加到没有门牌号、CO 和“/”(如果包含)的 writeline 地址吗?
猜你喜欢
  • 2021-04-07
  • 2012-04-06
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多