regex - 在 SAS 中使用 REGEX 匹配和删除重复出现的单词组
问题描述
我正在尝试删除 SAS 中的一组重复单词。基本上,我正在尝试删除重复出现的一组单词。正斜杠是分隔符。我正在使用 SAS 9.4 并有以下示例:
我尝试了上述正则表达式,它适用于“淋巴结疼痛/淋巴结疼痛/四肢疼痛”。结果是“淋巴结疼痛/四肢疼痛”。但是,它不适用于“淋巴结疼痛/四肢疼痛/四肢疼痛”和“淋巴结疼痛/神经痛/神经痛”。我不确定为什么。
data have; string = 'Lymph node pain/Pain in extremity/Pain in extremity';output; string = 'Lymph node pain/Lymph node pain/Pain in extremity'; output; string = 'Lymph node pain/Neuralgia/Neuralgia'; output; run; data test; set have; _1=prxparse('s/([A-Za-z].+?\s.*?\/.*?)(.*?)(\1+)/\2\3/i'); _2=prxparse('/([A-Za-z].+?\s.*?\/.*?)(.*?)(\1+)/i'); do i=1 to 10; string=prxchange(_1, -1, strip(string)); if not prxmatch(_2, strip(string)) then leave; end; drop i ; run;
任何帮助表示赞赏。
解决方案
这是一种scan
基于 - 的方法。我假设每个字符串最多有 3 个短语,但如果需要,可以轻松调整它以适用于任意数量的短语。
data have;
string = 'Lymph node pain/Pain in extremity/Pain in extremity';output;
string = 'Lymph node pain/Lymph node pain/Pain in extremity'; output;
string = 'Lymph node pain/Neuralgia/Neuralgia'; output;
string = 'Neuralgia/Lymph node pain/Neuralgia'; output; /*Added A/B/A example*/
run;
data test;
set have;
array phrases[3] $32;
/*Separate string into an array of phrases delimited by / */
do i = 1 to dim(phrases);
phrases[i] = scan(string,i,'/');
end;
/*Sort the array so that duplicate phrases are next to each other*/
call sortc(of phrases[*]);
/*Iterate through the array and build up an output string of non-duplicates*/
length outstring $255;
do i = 1 to dim(phrases);
if i = 1 then outstring = phrases[1];
else if phrases[i] ne phrases[i-1] then outstring = catx('/',outstring,phrases[i]);
end;
keep string outstring;
run;
这具有将所有短语排序为字母顺序而不是字符串中第一次出现的顺序的副作用。
推荐阅读
- apache-camel - Apache Camel - 如何在动态 sftp 端点中设置私钥
- c# - 如何在 IIS 上使用 pythonnet 运行 .NET Core 项目
- flutter - 使用flutter_webview_plugin加载页面后如何在WebView中执行JavaScript代码
- javascript - 优化函数以减少对数组的迭代
- python - 尝试向下滚动带有水平 ScrollView 的垂直 ScrollView 时出现 Kivy 问题
- python-3.x - 如何准确比较 Python 3 中的对象类型?
- typescript - 打字稿中的 Dialogflow 端点类型
- android - 自定义弹出菜单 - 如何?
- internet-explorer - 我的菜单和IE问题的响应式设计
- tensorflow - SageMaker 和 TensorFlow 2.0