簡體   English   中英

C#Regex.Split返回同一個單詞的多個實例

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

字符串: WITH Date = {GETDATE()} AND Customer = 8824

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

輸出:

WITH
WITH

Date
Date

=
=

{GETDATE()}
{GETDATE()}

AND
AND

Customer
Customer

=
=

8824
8824

顯然,所需的輸出是每個單詞的一個實例,而不是多個。 我沒有將任何標志。

模式是否有問題,或者我應該包含任何標志?

謝謝。

為什么選擇正則表達式?

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

如果您在[]加入括號或花括號/方括號作為第一個字符,則將其視為字面意義。

更簡單的是string.Split(..),如下所示:

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

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM