2014-09-12 23 views
0

我想在C#中使用正則表達式來打破字符串。 我具有例如字符串正則表達式來選擇NOT和操作數

{([Field] = '100' OR [LaneDescription] LIKE '%DENTINPALEUW%' 
OR [LaneDescription] = 'asdf' OR ([ObjectID] = 1) AND [ITEM_HEIGHT] >= 
10 AND [SENDER_COMPANY] NOT LIKE '%DHL%'} 

(從Telerik的RadFilter生成)

和我需要它破碎,所以我可以將其傳遞到與類型的自定義對象:開括號,場,比較器,值,左括號。

到目前爲止並與http://regexr.com的幫助下,我已經達到

\[([^\[\]]*)\]+|[\w'%]+|[()=] 

,但我需要得到「> =」和「NOT LIKE」作爲一個(和相似的價值觀一樣<>!=等..)

您可以在http://regexr.com/39g6b

看到我的深夜嘗試任何幫助將非常感激。

(PS:有在字符串沒有換行符)

+0

爲什麼要使用正則表達式,而不是使用字符串方法? C#非常適合用String類操作字符串。例如http://msdn.microsoft.com/en-us/library/2c7h58e5(v=vs.110).aspx – 2014-09-13 15:38:17

+0

因爲我需要它作爲一個字符串數組,如'> =','不喜歡','>' ,'('等....我不希望它作爲一個字符數組,因爲我將不得不創建大約100 ifs來做我想要的事 – ybdiel 2014-09-13 16:37:31

+0

您可以使用LINQ從數組中獲得所需內容 – 2014-09-13 16:38:44

回答

0

嘗試

\(|\)|\[[a-zA-Z0-9_]+\]|'.*?'|\d+|NOT LIKE|\w+|[=><!]+ 

Demo.

說明:

\(// match "(" literally 
    | // or 
    \) // ")" 
    | // or 
    \[[a-zA-Z0-9_]+\] // any words inside square braces [] 
    | 
    '.*?' // strings enclosed in single quotes '' (escape sequences can easily trip this up though) 
    | 
    \d+ // digits 
    | 
    NOT LIKE // "NOT LIKE", because this is the only token that can contain whitespace 
    | 
    \w+ // words like "NOT", "AND", etc 
    | 
    [=><!]+ // operators like ">", "!=", etc