如何使用KNIME从Excel的Description列中提取学生学号?
问题描述
我有一份包含多列的Excel数据,其中一列名为Description,数据示例如下:
Being Settlement of exam fee, student Number IMIS/C/5946/7/5/2021/5690 in respect of Flora Hamis
Being Settlement of school fee, student Number IMIS/C/28727/14/7/2022/28980 in respect of Tony Alex
Being Settlement of Life Assurance, student Number IMIS/C/28020/23/6/2022/28279 in respect of Richard Nixon
希望用KNIME提取每行中的学生学号,自己尝试的正则表达式如下:
regexReplace($Description$,^IMIS/\w{1}/\d/\d/\d/\d\s? ,"$1" )
解决方案
你的正则表达式存在几处问题,调整后即可正确提取学号:
- 未匹配
student Number前缀,无法准确定位学号起始位置 - 用
\d只能匹配单个数字,而学号里的数字段是多位的,需改为\d+ - 未处理学号前可能存在的多个空格情况
推荐两种可行的实现方式:
方式一:用regexMatcher提取捕获组
在KNIME的String Manipulation节点中使用以下表达式:
regexMatcher($Description$, "student Number\\s+(IMIS/C/\\d+/\\d+/\\d+/\\d+/\\d+)", 1)
说明:
student Number\\s+:匹配student Number及后续一个或多个空格(IMIS/C/\\d+/\\d+/\\d+/\\d+/\\d+):捕获完整学号,\\d+匹配任意长度的数字段- 末尾的
1指定提取第一个捕获组的内容
方式二:用regexReplace保留目标内容
如果坚持使用regexReplace,可以将无关内容替换为空,只保留学号:
regexReplace($Description$, "^.*student Number\\s+(IMIS/C/\\d+/\\d+/\\d+/\\d+/\\d+).*$", "$1")
说明:
^.*student Number\\s+:匹配从文本开头到student Number及后续空格的所有内容(IMIS/C/\\d+/\\d+/\\d+/\\d+/\\d+):捕获学号部分.*$:匹配学号之后的所有内容- 替换为
$1即可保留捕获到的学号
内容的提问来源于stack exchange,提问作者GASTONE DERECK
相关产品推荐
相关产品推荐

