简体   繁体   中英

c# Regex.Split returns multiple instances of the same word

String: WITH Date = {GETDATE()} AND Customer = 8824

Pattern: (([A-Za-z0-9@=><]+)|((\\(([^)]+)\\)))|(('[^,;]+'))|({[A-Za-z0-9@=><()]+}))+

Output:

WITH
WITH

Date
Date

=
=

{GETDATE()}
{GETDATE()}

AND
AND

Customer
Customer

=
=

8824
8824

Obviously, the desired output is one instance of each word and not multiple. I haven't included any flags.

Is there anything wrong with the pattern or should I include any flag?

Thanks.

Why Regex?

https://regexr.com/3isdf --> ([{}()A-Za-z0-9@=><]+)

If you include parentheses or curly-/square brackets inside [] as first characters, they are treated to be literally.

Simpler would be a string.Split(..) like this:

using System;

public class Program
{
    public static void Main()
    {
        var t = 
            "WITH Date = {GETDATE()} AND Customer = 8824"
            .Split(" ".ToCharArray(),            // add other unwanted chars to splitter
                   StringSplitOptions.RemoveEmptyEntries);  

        foreach(var part in t)
            Console.WriteLine(part);
    }
}

Output:

WITH
Date
=
{GETDATE()}
AND
Customer
=
8824

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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