英文:
OpenRefine: How to delete content of cells if it matches specific string pattern?
问题
问题是:我有一列,我想删除只包含特定模式并且只包含其他内容的单元格的内容。
我的列如下所示:
LF 2(1927)40
LF 2(1927)42
"Wirtin" LF 2(1927)44
Lottchen LF 3(1928)3
LF 3(1928)7
"Mit schönem Gruß Arthur Powalla aus Hamburg" LF 3(1928)9
LF 3(1928)14 DF 3(1927)1
我想删除所有仅包含字符串模式LF 2(1927)42
的单元格内容。如果它们包含更多内容,我希望保留一切而不删除该模式。
在该列的“Transform...”菜单中,我尝试使用以下GREL命令:
if(value==(LF \d\(\d*\)\d+),null,value)
我之前在另一个操作中使用了此命令中包含的正则表达式,它按预期运行。所以我认为错误在别处。
它报错如下:
Parsing error at offset 14: Missing )
英文:
So I'm doing my first project in OpenRefine and I don't fully get the GREL thing and how to transform my proeject with it yet...
Problem is: I have a column in which I want to delete ONLY the contents of the cells that match a specific pattern and ONLY if they contain nothing else.
My column looks like this: (image not allowed)
LF 2(1927)40
LF 2(1927)42
"Wirtin" LF 2(1927)44
Lottchen LF 3(1928)3
LF 3(1928)7
"Mit schönem Gruß Arthur Powalla aus Hamburg" LF 3(1928)9
LF 3(1928)14 DF 3(1927)1
I want to delete the content of all the cells that contain ONLY a string pattern like LF 2(1927)42
. If they contain more than that, I want to keep everything and not delete that pattern.
In the Transform... menue of that column I tried to use the following GREL command:
if(value==(LF \d\(\d*\)\d+),null,value)
I used the regular expression included in this before for a different operation, and it did what it was supposed to do. So I assume the mistake lies elsewhere.
The error it sends is this:
Parsing error at offset 14: Missing )
Thanks so much for helping me on this!!
答案1
得分: 0
模式匹配在赋值或比较表达式上是 GREL 不支持的功能。
你要找的表达式是
if(isNull(value.match(/LF \d\(\d*\)\d+/)), value, null)
请注意,我正在告诉 GREL 在单元格的内容 (value
) 上使用 match 函数,并尝试应用正则表达式,正则表达式用 /
括起来。
然后,我们通过 isNull 区分 null
(模式不适用)和空数组(模式适用,但模式没有定义子模式)。
或者,你也可以使用正则表达式的 文本筛选器,然后对筛选的数据执行转换。当你不太熟悉 GREL 时,这更直观。
英文:
Pattern matching on assignment or comparison expressions is a feature that is not supported by GREL.
The expression you are looking for is
if(isNull(value.match(/LF \d\(\d*\)\d+/)), value, null)
Note that I am telling GREL to use the match function on the content of the cell (value
) and try to apply the regular expression, enclosed in /
.
We then differentiate between null
(pattern does not apply) and an empty array (the pattern does apply, but the pattern does not have a sub pattern defined) via isNull.
Alternatively you could also use a text filter with regular expressions and then perform a transformation on the filtered data. This is more intuitive when you are not that familiar with GREL.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论