2012-05-21 53 views
2

我有一個字符串從駝峯字符串分割話

string = 'one Two9three four_Five 67SixSevenEightNine'; 

我需要把它拆分成的話:

'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' 

我設法除駝峯,所有分離時小寫字母后跟大寫:

while ~isempty(string) 
     [str,string] = ... 
      strtok(string, ... 
        [' [email protected]$/#.-:&*+=[]?!(){},''">_<;%' char(9) char(10) char(13) '0-9']); 
     str = regexprep(str, '[0-9]',''); 
end 

我也可以得到模式的索引,但只有當我知道如何插入空間或之間的一些字符,然後我可以使用上面的代碼再次分裂成一句話:

pattern = '[a-z][A-Z]+'; 
[pat,idx]=regexp(str, pattern,'match'); 

什麼想法? 謝謝!

回答

2

在做其他處理之前,爲什麼不更換駱駝套件?

newstring = regexprep(string, '([a-z])([A-Z])', '$1 $2'); 

while ~isempty(newstring) 
... 
+0

哇!看起來比我想象的要容易得多!謝謝! – Mallvina