IOS应用程序中的正则表达式字符串

时间:2016-12-23 07:11:31

标签: c# regex xamarin

我正在尝试将NSDictionary形式的字符串转换为字典,然后我必须通过方法:

string NSDictionaryConverter(string name)
        {
            foreach (var a in str)
            {
                if (a.Key.Description.Equals(name))
                {
                    result = a.Value.ToString();
                }
                Console.WriteLine(str.Keys);
            }
            return result;
        }

拿走我需要的东西。

为什么我要使用字典?这些字典包含从地图中包含注释的所有内容的信息。

Key FormattedAddressLines例如:

FormattedAddressLines =     (
        "ZIP City Name",
        Country
    );

我遇到问题的价值是地址,因为它包含很多细节。我需要在屏幕上很好地显示它们。

即,我需要在标点符号前删除带有空格的"()字符和换行符。

在正则表达式之后,它看起来仍然很混乱:

enter image description here

string address = NSDictionaryConverter("FormattedAddressLines");
                string city = NSDictionaryConverter("City");
                string zip = NSDictionaryConverter("ZIP");
                string country = NSDictionaryConverter("Country");
                address = Regex.Replace(address, @"([()""])+", "");
                fullAddress = address + ", " + city + ", " + zip + ", " + country;
                addressLabel.Text = fullAddress; 

我怎么能这样做:

完整地址值, - 新行 XXX, - 换行 XXX, - 新线 ... - 新队 N值 - 新行

1 个答案:

答案 0 :(得分:1)

在标点符号之前,您似乎需要删除特定的特殊字符和空格。

您需要为正则表达式添加\s*(?:\r?\n|\r)\s*(?=\p{P})替代方法:

Regex.Replace(address, @"[()""]+|\s*(?:\r?\n|\r)+\s*(?=\p{P})", "")
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

\s*匹配0 +空格,(?:\r?\n|\r)+匹配1个或多个换行符,\s*(?=\p{P})匹配0 +空格,后跟标点符号。如果您还想要包含符号,则可能需要将\p{P}替换为[\p{P}\p{S}]

请参阅https://git-for-windows.github.io/

regex demo