我有一个字符串
string = 'one Two9three four_Five 67SixSevenEightNine';
我需要将其拆分为以下单词:
'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine'
当小写字母后面跟着大写字母时,我设法将除 CamelCase 之外的所有内容分开:
while ~isempty(string)
[str,string] = ...
strtok(string, ...
[' ~@$/#.-:&*+=[]?!(){},''">_<;%' 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');
有什么想法吗? 谢谢!
最佳答案
为什么不在进行其他处理之前替换camelCase?
newstring = regexprep(string, '([a-z])([A-Z])', '$1 $2');
while ~isempty(newstring)
...
关于regex - 从 CamelCase 字符串中拆分单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10692971/