2011-11-14 81 views
0

進出口試圖取代PHP下面的字符串之間:更換一個空間,是一個字母和數字之間用逗號

2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres 46521,Pyroxeres 

有了:

2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres,46521,Pyroxeres 

注:有5逗號分隔這裏值

Time, Character, Item Type, Quantity, Item Group 

注仍有待留下一些空間落後,如日期和時間以及第一和最後n之間ame和其名稱中包含多個詞的項目。

此外,它不能代替\ r和\ n因爲有許多行像上面

回答

1

下面是搜索的正則表達式:

(\d{4}\.\d{2}\.\d{2}\s\d{2}:\d{2},[^,]*,\D*)\s(\d{5},.*) 

而正則表達式替換

$1,$2 

說明:

正則表達式搜索

(  Begin a numbered capture group (Nameless and accessed via index) 
\d{4} Means: capture digits with exact 4 repititions 
\.  Capture a dot (You have to escape it with a backslash, if not it means capture all 
     characters except linefeed) 
\d{2} Capture 2 digits 
\.  Capture a dot 
\d{2} Capture 2 digits 
\s  Capture 1 whitespace 
\d{2} Capture 2 digits 
:  Capture a colon 
\d{2} Capture 2 digits 
,  Capture a comma 
[^,]* Capture everything except a comma with 0 to infinity repititions 
,  Capture a comma 
\D*  Capture everything except a digit with 0 to infinity repititions 
)  Close first capture group 
\s  Capture a whitespace 
(  Begin a numbered capture group 
\d{5} Capture 5 digits 
,  Capture a comma 
.*  Capture everything except a linebreak 
)  Close second capture group 

的正則表達式替換

$1  Insert capture group number 1 
,  Insert a comma 
$2  Insert capture group number 2 

我希望說明一點正則表達式(有用的,但有時也令人困惑)「神奇」。設計和測試正則表達式的好工具叫做Expresso,可以找到here。如果你想要一個在線幫助,並表達測試regex庫,你會發現它here

+0

這就像一個魅力謝謝你。你認爲你可以把像Narenda yadala dod這樣的解釋,讓其他讀者可以理解(與我一起) – Wes

+0

@Wes:我在我的答案中添加了對正則表達式的解釋。 – Fischermaen

+0

非常感謝。像這樣的答案值得更多+ 1。謝謝你的迴應。 – Wes

0

的一個你可以使用這個

$result = preg_replace('/(?<=[a-z_])\s+(?=\d)|(?<=\d)\s+(?=[a-z_])/i', ',', $subject); 

說明

   # Match either the regular expression below (attempting the next alternative only if this one fails) 
    (?<=   # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) 
     [a-z_]   # Match a single character present in the list below 
         # A character in the range between 「a」 and 「z」 
         # The character 「_」 
    ) 
    \s    # Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) 
     +    # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
    (?=    # Assert that the regex below can be matched, starting at this position (positive lookahead) 
     \d    # Match a single digit 0..9 
    ) 
|    # Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) 
    (?<=   # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) 
     \d    # Match a single digit 0..9 
    ) 
    \s    # Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) 
     +    # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
    (?=    # Assert that the regex below can be matched, starting at this position (positive lookahead) 
     [a-z_]   # Match a single character present in the list below 
         # A character in the range between 「a」 and 「z」 
         # The character 「_」 
    ) 
+0

,這看起來像它的工作除了它取代了\ r \ n個字符以及 – Wes

+0

@Wes如果你不希望這樣的事情發生,然後替代'[A-ZA-Z_]'代替'\ d '。我編輯了答案來解決'\ D'問題。 –

+0

效果相同。它仍然擺脫了新的字符。我嘗試添加[^ \ S \ r \ n],但之後又搞亂了其他一些東西。 – Wes

0

在perl你可以使用:

$line =~ s/([A-Za-z])\s+(\d)/$1,$2/g; 
0

你不說你使用什麼語言,但這樣的事情應該工作:

搜索:([A-Za-z]) (\d) 更換:\1,\2

0

如何:

$str = "2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres 46521,Pyroxeres"; 
$str = preg_replace('/(\D+) (\d+)/', "$1,$2", $str); 
echo $str; 

輸出:

2011.11.12 20:06,Teal'c Ostus,Solid Pyroxeres,46521,Pyroxeres 
相關問題