2011-09-22 26 views
4

字符串尋找正則表達式的解決方案如下scenaio:尋找正則表達式來拆分上大寫的基礎

我有一個字符串,我已經分裂大寫的基礎,但連續的大寫部分上不應該分裂。

例如:如果輸入的是

DisclosureOfComparativeInformation 

O/p應該

Disclosure Of Comparative Information 

但連續大寫不應該分裂。

GAAP不應導致G A A P

如何找到特定的圖案和插入空間?

感謝名單

+0

你要拆分的字時,纔會有一個小寫,一個大寫字母? –

回答

7

嘗試 -

var subjectString = "DisclosureOfComparativeInformation"; 
var resultString = Regex.Replace(subjectString, "([a-z])([A-Z])", "$1 $2"); 
0

使用正則表達式解決方案來查找r字符串,其中不是真正趨於變得無法識別。我建議你在循環中瀏覽你的字符串,並相應地拆分它,而不使用正則表達式。

0

在Perl這應該工作:

str =~ s/([A-Z][a-z])/ \1/g; 

圍繞這兩個字符集的括號挽救賽的「\ 1」後(一把手)。如果替換爲匹配+空間

+1

不會這隻匹配前兩個字符? – smitec

+0

@smitec感謝您的支持。我在末尾添加了'g'修飾符,它替換了所有出現的模式(在Perl中)。另外,這個解決方案需要修剪,就像sikender的回答一樣。 – jcadcell

0

[A-Z]{1}[a-z]+

將分裂如下

DisclosureOfComparativeInformation -> Disclosure Of Comparative Information

GAPS -> GAPS

SOmething -> SOmething這一個可能是不期望

alllower -> alllower

0

斯普利特和加入:

string.Join(" ", Regex.Split("DisclosureOfComparativeInformation", @"([A-Z][a-z]*)"))