Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用MATLAB同时替换单元数组中的多个子字符串_Regex_Matlab - Fatal编程技术网

Regex 如何使用MATLAB同时替换单元数组中的多个子字符串

Regex 如何使用MATLAB同时替换单元数组中的多个子字符串,regex,matlab,Regex,Matlab,我被要求获取一个条形码符号序列,并用实际的数字表示替换每个数字子字符串。换句话说,条形码序列中的数字'1'由'…::'表示。每个数字[0-9]由5个字符的符号表示。每个条形码由10位数字组成,因此我想创建一个与符号条形码字符串等效的数字字符串 前- “…:::::::::::::::::::::::”相当于1910499980 我试图完成的是捕获每个5个字符的序列,用它的等效数字替换它,然后继续下一个5个字符的序列。到目前为止,我的代码如下,但它不能正常工作,它替换了一些符号,但不是全部。它还错

我被要求获取一个条形码符号序列,并用实际的数字表示替换每个数字子字符串。换句话说,条形码序列中的数字
'1'
'…::'
表示。每个数字[0-9]由5个字符的符号表示。每个条形码由10位数字组成,因此我想创建一个与符号条形码字符串等效的数字字符串

前-

“…:::::::::::::::::::::::”
相当于
1910499980

我试图完成的是捕获每个5个字符的序列,用它的等效数字替换它,然后继续下一个5个字符的序列。到目前为止,我的代码如下,但它不能正常工作,它替换了一些符号,但不是全部。它还错误地替换了一些数字。我忘了提到每行的第一个字符都要省略,所以我的代码的一部分已经考虑到了这一点

你能提供的任何帮助都会很好

谢谢, 丹


试试这个-

功能

function output_string = barcode_seq(input_string)

%%// Params
pat1 = '...::';
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0];

barcode_len = size(patterns,2);

s1 = reshape(input_string,barcode_len,[])';
comp_exp_mat  = bsxfun(@eq,patterns,permute(s1,[3 2 1]));
[x,y] = find(squeeze(sum(comp_exp_mat,2))==barcode_len);
x(x==10)=0;
output_string = num2str(x)';
>> barcode_seq('...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...')

ans =

1910499980
跑步

function output_string = barcode_seq(input_string)

%%// Params
pat1 = '...::';
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0];

barcode_len = size(patterns,2);

s1 = reshape(input_string,barcode_len,[])';
comp_exp_mat  = bsxfun(@eq,patterns,permute(s1,[3 2 1]));
[x,y] = find(squeeze(sum(comp_exp_mat,2))==barcode_len);
x(x==10)=0;
output_string = num2str(x)';
>> barcode_seq('...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...')

ans =

1910499980
该课程将用于此目的:

>> nums = [1:9 0];
>> patterns = {'...::';'..:.:';'..::.';'.:..:';'.:.:.';...
             '.::..';':...:';':..:.';':.:..';'::...'};
>> map = containers.Map(patterns,vals)

map = 
  Map with properties:
        Count: 10
      KeyType: char
    ValueType: double
使用该方法根据一个或多个输入模式查找值:

>> codeTest = patterns([5 4 7 1]);
>> map.values(codeTest)'
ans = 
    [5]    [4]    [7]    [1]
将字符串分割为单元格,现在您可以转换所有条形码值:

bc = cell2mat(barcodes); bc = bc(:,2:end);
codes5 = mat2cell(bc,ones(size(bc,1),1),5*ones(1,size(bc,2)/5));
codesNumeric = cell2mat(map.values(codes5))

重塑字符串的形状,使每组五个符号位于不同的行上,并使用
ismember
(使用
“行”
选项)查找每行匹配的模式:

str = '...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...'; %// example string
%// Variables "patterns" and "numbers" are defined in your code

[~, ind] = ismember(reshape(str,5,[]).', patterns, 'rows');
result = numbers(ind).';

result =
1910499980

使用初始条形码变量、模式变量和数字变量,此代码将读取条形码中的每一行。然后,它将单元格数组转换为字符数组,删除第一个字符,创建新的单元格数组,将单元格数组拆分为5个符号段,并将每个段替换为对应的数字段。最后一步是将新的数字字符串组合成一个类似于原始条形码数组的矩阵

barcodes = {':...:::.:.....::::....:..::.:..:.:..:.:..:..:.::...';
':::...:..:...::...::.::.....:.:.:..:..::...:.:..:.:';
'::.:...:..:.::..::...:...:::.....::...::.::...:..:.';
'::...:.:..:::...:...::..:...:.:..::.:..:..:..::...:';
':::....::...:.:...:.:::...:.:..:.:..::...::...:.:..';
':.:..:..::...:.:...::::......:::...:.:..:.::..:...:';
'::.:..:..:....::..:.:..:.:.:..:..::..:.:.:..:.:..:.';
':.::.....:::...:.::.....::..::..:..:.:..:.:..:.:..:';
'::...::..:..::...::...::..:...:.:.:.:...:.:.:...::.';
'::..:...::...:.:::......:::..:...::..:..:..:.::.:..';
'::..:...:.:::...:...:...::.:..:.:..:.:..:.:..:.::..';
':.:..::..:.:..:...:.:.:..::.:..:.:..:.:..:..:.:.:..';
':..::...:.:..::.::....::....::..:..:..:.:.:.:...:.:';
':..::..::.....::::....:..:.:.:.:.:..:..:...::....::';
':.::...::...:.:.::....::....:.:::.....:.:..::.:...:';
'::..:..:..:..::...:.:..:.:.:..:..::.:.:...:..:...::';
':.:.:.:...:::...::...:...:..::..:..:..:.:.:..::..:.';
':...::..:.:..:.:.:..:.::....::.:..:.:..:.:..:.:..:.';
':..:.:..::.:..:.::....::..:...:.:..:..::...:.::...:';
':.:..::...::..:.::...:.:....::.:..:...:.:...:::..:.';
'::..:..:.:.::...::....:..:.:..:..::..:.:..:..::...:';
'::..:.::....:.:...:.:..::.:..:..:..:..::...:.:.:.:.';
':.:.:..::..::...::......:::..:..:..:.:.:..:.:..::..';
':..::.:...:..:.:::...:.:.....::..:.:...::..::...:.:';
':.:..:::....::..::......::.::...::...:.:..:..::..:.';
':..:.::...:.::..:.:...:.:..:..:..::...:.::.:....::.';
':.::...:.:.:..:.:.:..:...:.:..::.:....::...:.::...:';
':..::.::.....::.::.....::.:..:..:..::...:.:..::..:.';
':::.....:.:...::..::.:.:..::...::...::...::....:.:.';
':::...:..:..:.:..:..:::....:..:.:.:..:.:...::.:...:';
':::.....:.::.:..::.....::..:..:..::...:.::.:..:..:.';
'::..:.:.:..::...::.....:.:.::..::....::..::...:.:..';
':.:.:.::...::......::...::..::..:..:.:..:..:.:::...';
':.:.:...::.:...:::....::..:.:....::...:.:...::.:..:'};
pat1 = '...::';%patterns corresponding to each barcode sequence
pat2 = '..:.:';
pat3 = '..::.';
pat4 = '.:..:';
pat5 = '.:.:.';
pat6 = '.::..';
pat7 = ':...:';
pat8 = ':..:.';
pat9 = ':.:..';
pat0 = '::...';
num1 = '1'; % variables for each number
num2 = '2';
num3 = '3';
num4 = '4';
num5 = '5';
num6 = '6';
num7 = '7';
num8 = '8';
num9 = '9';
num0 = '0';
split = [];

patterns = [pat1;pat2;pat3;pat4;pat5;pat6;pat7;pat8;pat9;pat0]; %character matrix for comparison
numbers = [num1;num2;num3;num4;num5;num6;num7;num8;num9;num0]; %character matrix for replacement

patternCell = cellstr(patterns); %convert patterns to cell array
numberCell = cellstr(numbers); %convert numbers to cell array

barcodeStrings = char(barcodes); %convert to character array
barcodeStrings = barcodeStrings(:,2:end);
barcodeStrings = cellstr(barcodeStrings);

for i = 1:length(barcodes)
    newBarcodes = regexp(barcodeStrings, '\W{1,5}', 'match');
end

for j = 1:length(newBarcodes)
    split = [split; regexprep(newBarcodes{j}, patternCell, numberCell)];
end

numbMat = cell2mat(split);
输出:

disp(numbMat)
1910499980
0833024322
9460703308
7407823847
0652099009
4321017467
9812243588
6176134444
7866675753
8320183429
8207144446
4882499989
3230634252
3610459831
6650620237
8432243941
5700734248
1224638888
2380674327
4780938218
8500443547
8052384325
5600184556
3720912132
4060166548
2769543293
6589749327
3030384748
0213900005
0854045537
0290343298
8900260609
5001134420
5370693214

您好,chappjc,您的回答非常好,当我运行代码时,它工作得非常好。唯一的问题是,这是我的第一门MATLAB课程,使用map.values还没有介绍给我们。因此,我很可能无法使用此代码。我成功地完成了代码,并将在其他帖子中提供它。谢谢你的帮助。