2010-06-08 42 views
4

我有以下的輸入字符串:正則表達式消除空格但不包括裏面的「」

key1 = "test string1" ; key2 = "test string 2" 

我需要將其轉換爲以下無標化

key1="test string1";key2="test string 2" 
+1

可以引用引號嗎? – SLaks 2010-06-08 14:07:18

+0

你在用什麼語言? – SLaks 2010-06-08 14:08:40

+0

值(在引號內)是否也包含'='和';'? – 2010-06-08 14:09:46

回答

2

使用ERE,即擴展正則表達式(在這種情況下比基本RE更清晰),假設沒有引用轉義並具有全局標誌(替換所有出現的內容),可以這樣做:

s/ *([^ "]*) *("[^"]*")?/\1\2/g

的sed:

$ echo 'key1 = "test string1" ; key2 = "test string 2"' | sed -r 's/ *([^ "]*) *("[^"]*")/\1\2/g' 

C#代碼:

using System.Text.RegularExpressions; 
Regex regex = new Regex(" *([^ \"]*) *(\"[^\"]*\")?"); 
String input = "key1 = \"test string1\" ; key2 = \"test string 2\""; 
String output = regex.Replace(input, "$1$2"); 
Console.WriteLine(output); 

輸出:

key1="test string1";key2="test string 2" 

逃生感知版本

在我達到那個沒有表現出可能導致不正確的結果正則表達式的逃生意識版本的結論第二個想法,所以在這裏它是:

s/ *([^ "]*) *("([^\\"]|\\.)*")?/\1\2/g

這在C#中的樣子:

Regex regex = new Regex(" *([^ \"]*) *(\"(?:[^\\\\\"]|\\\\.)*\")?"); 
String output = regex.Replace(input, "$1$2"); 

請不要從這些反斜槓盲目!

Input: key1 = "test \\ " " string1" ; key2 = "test \" string 2" 
Output: key1="test \\ "" string1";key2="test \" string 2" 
+0

@prxemoc:你使用的是* Perl衍生的正則表達式(通常稱爲「Perl兼容」或「PCRE」); ERE完全是另一回事。你可以通過使用C#的逐字串文字來減輕反斜線:'@「*([^」「] *)*(」「(?:[^ \\」「] | \\。)*」「) ?「' – 2010-06-08 21:09:08

+0

@Alan Moore:是的,沒有。我的類似sed的表達式的RE部分是EREs,請參閱[POSIX @ regular-expressions.info](http://www.regular-expressions.info/posix.html)。但它們也是Perl RE,因爲Perl RE是擴展的ERE(幾乎部分ERE在Perl RE中不可用,但這些功能實際上從未使用過),請參見[Regular Expression Flavor Comparison @ regular-expressions.info](http ://www.regular-expressions.info/refflavors.html)。它與.NET RE類似(是的,這些RE是Perl派生的RE)。請不要做出匆忙的判斷。感謝'@'-tip! – przemoc 2010-06-09 00:35:54

+0

重點。我看着你的sed代碼,並看到Perl。 – 2010-06-09 01:14:22

5

你會好得多不使用正則表達式。

你應該做的是解析字符串。你所描述的問題是一種迷你語言,因爲該字符串中的每個點都有一個狀態(例如「在引用字符串中」,「在關鍵部分」,「賦值」)。

例如,當您決定要轉義角色時會發生什麼?

key1="this is a \"quoted\" string" 

沿着字符串逐個字符移動,保持並隨時更改狀態。根據狀態,您可以發出或省略您剛纔閱讀的字符。

作爲獎勵,您將獲得檢測語法錯誤的能力。