我想知道是否有办法在 Octave / Matlab 中进行模式匹配?我知道 Maple 10 有执行此操作的命令,但不确定我需要在 Octave / Matlab 中做什么。所以如果一个数字是12341234123412341234
模式匹配将是1234
。我正在努力寻找重复生成整个字符串的最短模式.
请注意:数字(仅使用数字)不会这么简单。还,我不会提前知道模式(这就是我正在寻找的)。请参阅枫树 10 示例下面显示该模式事先未知,但该命令找到了该模式。
Maple 10 模式匹配示例:
ns:=convert(12341234123412341234,string);
ns := "12341234123412341234"
StringTools:-PrimitiveRoot(ns);
"1234"
我怎样才能在 Octave / Matlab 中做到这一点?
PS:我使用的是Octave 3.8.1
要找到重复生成整个字符串的最短模式,您可以使用正则表达式,如下所示:
result = regexp(str, '^(.+?)(?=\1*$)', 'match');
一些例子:
>> str = '12341234123412341234';
>> result = regexp(str, '^(.+?)(?=\1*$)', 'match')
result =
'1234'
>> str = '1234123412341234123';
>> result = regexp(str, '^(.+?)(?=\1*$)', 'match')
result =
'1234123412341234123'
>> str = 'lullabylullaby';
>> result = regexp(str, '^(.+?)(?=\1*$)', 'match')
result =
'lullaby'
>> str = 'lullaby1lullaby2lullaby1lullaby2';
>> result = regexp(str, '^(.+?)(?=\1*$)', 'match')
result =
'lullaby1lullaby2'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)