简体   繁体   English

地址上的C#RegEx无法正常工作

[英]C# RegEx on address doen't work properly

So I need the address out of a string, but I have different cases, so I covered many, but now I can't get working this case: 所以我需要一个字符串的地址,但我有不同的情况,所以我涵盖了很多,但现在我无法解决这个问题:

Original String: 原始字符串:


HRB 145942: playloop UG (haftungsbeschränkt), Hamburg, Eimsbütteler Chaussee 57, c/o Jan Sorgenfrei , 20259 Hamburg. HRB 145942:playloop UG(haftungsbeschränkt),汉堡, EimsbüttelerChaussee57,c / o Jan Sorgenfrei ,20259汉堡。 Gesellschaft mit beschr Gesellschaft mit beschr


HRB 145941: TBE-Ehrbare Rebellen GmbH, Hamburg, Friedrich-Ebert-Damm 111 , 22047 Hamburg. HRB 145941:TBE-Ehrbare Rebellen GmbH,Hamburg, Friedrich-Ebert-Damm 111,22047 Hamburg。 Gesellschaft mit beschr Gesellschaft mit beschr


HRB 145923: Lionheart Asset Management GmbH, München, Trenknerweg 100 A, c/o Barthold Brümmer , 22605 Hamburg. HRB 145923:Lionheart Asset Management GmbH,München, Trenknerweg 100 A,c / oBartholdBrümmer ,22605 Hamburg。 Gesellschaft mit beschr Gesellschaft mit beschr


HRB 716239: CarboCode Germany GmbH, Konstanz, Byk-Gulden-Straße 2, Gebäude F21 , 78467 Konstanz. HRB 716239:CarboCode Germany GmbH,Konstanz, Byk-Gulden- Straße2,GebäudeF21,78467Konstanz。 Gesellschaft mit beschr Gesellschaft mit beschr

So I need " Eimsbütteler Chaussee 57, c/o Jan Sorgenfrei " and " Friedrich-Ebert-Damm 111 " and " Trenknerweg 100 A, c/o Barthold Brümmer " and " Byk-Gulden-Straße 2, Gebäude F21 " 所以我需要“ EimsbüttelerChaussee57,c / o Jan Sorgenfrei ”和“ Friedrich-Ebert-Damm 111 ”和“ Trenknerweg 100 A,c / oBartholdBrümmer ”和“ Byk-Gulden- Straße2,GebäudeF21

Even just the address without the c/o would help me a lot My RegEx is: , \\d{5}.+\\. 即使只是没有c / o的地址也会帮助我很多My RegEx: , \\d{5}.+\\.

And my problem is, that it gets normal addresses like "Friedrich.." but in the "c/o" addresses it just takes like "c/o Jan Sorgenfrei" and missing the string before or it just takes the "Gebäude F21". 我的问题是,它获得了像“弗里德里希......”这样的正常地址,但是在“c / o”地址中,它只是像“c / o Jan Sorgenfrei”那样,并且之前错过了字符串或它只是采用了“GebäudeF21” 。

After getting the string from the RegEx I do the following to filter out some things: 从RegEx获取字符串后,我执行以下操作来过滤掉一些内容:

stadt = stadt.Substring(0, stadt.LastIndexOf('.'));
stadt = new string(stadt.Where(c => c != '-' && (c < '0' || c > '9')).ToArray());
stadt = stadt.Substring(1);

Provided that format always remains the same you might not need ReGex here. 如果格式始终保持不变,那么您可能不需要ReGex。 Note that this will take the city part as well : 请注意,这也将采取城市部分:

        var addressList = new[]
        {
            "HRB 145942: playloop UG (haftungsbeschränkt), Hamburg, Eimsbütteler Chaussee 57, c/o Jan Sorgenfrei, 20259 Hamburg. Gesellschaft mit beschr",
            "HRB 145941: TBE - Ehrbare Rebellen GmbH, Hamburg, Friedrich-Ebert - Damm 111, 22047 Hamburg.Gesellschaft mit beschr",
            "HRB 145923: Lionheart Asset Management GmbH, München, Trenknerweg 100 A, c / o Barthold Brümmer, 22605 Hamburg.Gesellschaft mit beschr",
            "HRB 716239: CarboCode Germany GmbH, Konstanz, Byk - Gulden - Straße 2, Gebäude F21, 78467 Konstanz.Gesellschaft mit beschr"
        };

        var addresses = addressList.Select(
            s =>
            {
                var secondIndexOfComma = s.IndexOf(',', s.IndexOf(',') + 1);
                return s.Substring(secondIndexOfComma + 2, s.LastIndexOf('.') - secondIndexOfComma - 2);
            });

        foreach (var address in addresses)
        {
            Console.WriteLine(address);
        }

Output: 输出:

Eimsbütteler Chaussee 57, c/o Jan Sorgenfrei, 20259 Hamburg
Friedrich-Ebert - Damm 111, 22047 Hamburg
Trenknerweg 100 A, c / o Barthold Brümmer, 22605 Hamburg
Byk - Gulden - Straße 2, Gebäude F21, 78467 Konstanz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM