以点作为分隔符的分割字符串,值也为点

时间:2019-06-26 05:26:39

标签: c#

我下面的字符串值不同

格式为

“某些字符串” “某些字符串” “我要捕获的内容(可能包含点)” “选择字符串”

var str1 = "XXX.XXX.test.com.X1";
var str2 = "Y.YY.google.co.in.X2";
var str3 = "ZZ.ZZZ.google.co.in";
var str4 = "PPPP.P.Yahoo";

我想捕获的地方

test.com 来自str1

来自str2的

google.co.in

来自str3的

google.co.in

雅虎来自str4

我在下面尝试过

var str4 = "PPPP.P.Yahoo";
        var part = str4.Split(new char[] { '.' }, 4);
        var result = part[2];

只有在str4之间有点(test.com等),这样才能为Undefined symbols for architecture arm64: "_OBJC_CLASS_$_FIRDynamicLinks", referenced from: objc-class-ref in libFirebaseCppApp.a(invites_receiver_internal_ios_aa7b75b2efc3269e88f1077e55250e5a.o) ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 提供正确的结果。

有什么优雅的分割方式?

1 个答案:

答案 0 :(得分:-1)

尝试一下,可能会得到一些帮助。

public static void Main()
{
    var str1 = "XXX.XXX.test.com.X1";
    var str2 = "Y.YY.google.co.in.X2";
    var str3 = "ZZ.ZZZ.google.co.in";
    var str4 = "PPPP.P.Yahoo";
    var str5="XX.XXX.test.com.Y1";
    var str6="Y.YY.google.co.in.X2";
    var str7 ="Y.YY.google.co.in.XX";

    var regex = new Regex(@".[XY][0-9a-zA-Z](.*)", RegexOptions.Singleline);

    str1=string.Join(".", str1.Split('.').Skip(2).ToArray());
    str2=string.Join(".", str2.Split('.').Skip(2).ToArray());
    str3=string.Join(".", str3.Split('.').Skip(2).ToArray());
    str4=string.Join(".", str4.Split('.').Skip(2).ToArray());
    str5=string.Join(".", str5.Split('.').Skip(2).ToArray());
    str6=string.Join(".", str6.Split('.').Skip(2).ToArray());
    str7=string.Join(".", str7.Split('.').Skip(2).ToArray());

     Console.WriteLine(regex.Replace(str1, ""));
     Console.WriteLine(regex.Replace(str2, ""));
     Console.WriteLine(regex.Replace(str3, ""));
     Console.WriteLine(regex.Replace(str4, ""));
     Console.WriteLine(regex.Replace(str5, ""));
     Console.WriteLine(regex.Replace(str6, ""));
     Console.WriteLine(regex.Replace(str7, ""));
}

也请参见此demo

Output like below:

test.com
google.co.in
google.co.in
Yahoo
test.com
google.co.in
google.co.in