OpenRefine:如何删除单元格内容,如果它匹配特定的字符串模式?

huangapple go评论56阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月19日 17:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定