Matlab:使用单词列表查找字符串模式并用列表中的一个单词替换文本
Matlab: Find string pattern with a list of words and replace in text with one word of the list
在 Matlab 中,考虑字符串:
str = 'text text text [[word1,word2,word3]] text text'
我想从列表('word1'、'word2'、'word3')中随机分离出一个词,比如 'word2',然后写一个可能是新的文件,字符串:
strnew = 'text text text word2 text text'
我的做法是这样的(肯定很差):
隔离字符串“[[word1,word2,word3]]”可以通过
实现
str2=regexp(str,'\[\[(.*?)\]\]','match')
删除字符串中的左方括号和右方括号是通过
实现的
str3=str2(3:end-2)
最后我们可以将str3拆分成一个单词列表(存储在一个单元格中)
ListOfWords = split(str3,',')
输出 {'word1'}{'word2'}{'word3'}
而我被困在那里。我怎样才能选择其中一个条目并将其插回初始字符串(或它的副本......)?请注意,如果有帮助,分隔符 [[
和 ]]
都可以更改为 ||
。
您可以按照以下方式进行:
- 使用
regexp
和 'split'
选项;
- 将中间部分拆分为单词;
- Select一个随机词;
- 向后连接。
str = 'text text text [[word1,word2,word3]] text text'; % input
str_split = regexp(str, '\[\[|\]\]', 'split'); % step 1
list_of_words = split(str_split{2}, ','); % step 2
chosen_word = list_of_words{randi(numel(list_of_words))}; % step 3
strnew = [str_split{1} chosen_word str_split{3}]; % step 4
我有一个糟糕的解决方案。我试图看看我是否可以在一个函数调用中完成它。你可以……但代价是什么!像这样滥用动态正则表达式几乎算作一次函数调用。
我使用动态表达式来处理逗号分隔列表。棘手的部分是选择一个随机元素。这变得非常困难,因为 MATLAB 的语法不支持对函数调用结果进行 paren 索引。为了解决这个问题,我把它放在一个结构中,这样我就可以点索引。这太可怕了。
>> regexprep(str,'\[\[(.*)\]\]',"${struct('tmp',split(string(),',')).tmp(randi(count(,',')+1))}")
ans =
'text text text word3 text text'
Luis 肯定是最好的答案,但我认为不使用正则表达式可以简化一点。
str = 'text text text [[word1,word2,word3]] text text'; % input
tmp = extractBetween(str,"[[","]]"); % step 1
tmp = split(tmp, ','); % step 2
chosen_word = tmp(randi(numel(tmp))) ; % step 3
strnew = replaceBetween(str,"[[","]]",chosen_word,"Boundaries","Inclusive") % step 4
在 Matlab 中,考虑字符串:
str = 'text text text [[word1,word2,word3]] text text'
我想从列表('word1'、'word2'、'word3')中随机分离出一个词,比如 'word2',然后写一个可能是新的文件,字符串:
strnew = 'text text text word2 text text'
我的做法是这样的(肯定很差):
隔离字符串“[[word1,word2,word3]]”可以通过
实现str2=regexp(str,'\[\[(.*?)\]\]','match')
删除字符串中的左方括号和右方括号是通过
实现的str3=str2(3:end-2)
最后我们可以将str3拆分成一个单词列表(存储在一个单元格中)
ListOfWords = split(str3,',')
输出 {'word1'}{'word2'}{'word3'}
而我被困在那里。我怎样才能选择其中一个条目并将其插回初始字符串(或它的副本......)?请注意,如果有帮助,分隔符 [[
和 ]]
都可以更改为 ||
。
您可以按照以下方式进行:
- 使用
regexp
和'split'
选项; - 将中间部分拆分为单词;
- Select一个随机词;
- 向后连接。
str = 'text text text [[word1,word2,word3]] text text'; % input
str_split = regexp(str, '\[\[|\]\]', 'split'); % step 1
list_of_words = split(str_split{2}, ','); % step 2
chosen_word = list_of_words{randi(numel(list_of_words))}; % step 3
strnew = [str_split{1} chosen_word str_split{3}]; % step 4
我有一个糟糕的解决方案。我试图看看我是否可以在一个函数调用中完成它。你可以……但代价是什么!像这样滥用动态正则表达式几乎算作一次函数调用。
我使用动态表达式来处理逗号分隔列表。棘手的部分是选择一个随机元素。这变得非常困难,因为 MATLAB 的语法不支持对函数调用结果进行 paren 索引。为了解决这个问题,我把它放在一个结构中,这样我就可以点索引。这太可怕了。
>> regexprep(str,'\[\[(.*)\]\]',"${struct('tmp',split(string(),',')).tmp(randi(count(,',')+1))}")
ans =
'text text text word3 text text'
Luis 肯定是最好的答案,但我认为不使用正则表达式可以简化一点。
str = 'text text text [[word1,word2,word3]] text text'; % input
tmp = extractBetween(str,"[[","]]"); % step 1
tmp = split(tmp, ','); % step 2
chosen_word = tmp(randi(numel(tmp))) ; % step 3
strnew = replaceBetween(str,"[[","]]",chosen_word,"Boundaries","Inclusive") % step 4