Skip to content

Add 2 new GREL functions for replacing strings matching a search list #2606

Description

@thadguidry

Is your feature request related to a problem or area of OpenRefine? Please describe.
Currently we do not have a nice single replace() function that takes a List of Strings as an argument to search for in a String and replace any found match with a replacement character.
We have to resort to use of cross() or forEach() with inArray() to perform the lookup/inspection of a List or Array and then perform our String replacement.

Mailing list thread comment:
https://groups.google.com/d/msg/openrefine/XIfKTL6sv0w/BjUedqn0AgAJ

GIVEN: 2 columns where column A has the string we want to replace when a list of substrings are found.
AND: column B has a list (or can be split into an Array) of those substrings to search for
AND: and when matching,
THEN: replace ANY matching substring in our column A with a single replacement value like "" empty string or whatever the user desires.

The following GREL performs this:

forEach(
  forEach(
    value.split(","),
    i,
    i.trim()
  ),
  i,
  if(
    inArray(cells.B.value.split("|"),i),
    "",
    i
    )
)
.join(",")

however, it is very verbose.

Describe the solution you'd like
Instead we can probably simplify this large expression to something like this that takes as an argument (a list of strings to search for) and those that match get replaced with a replacement char.

value.trim().replaceEach(cells.B.value.split("|"), "")

Describe alternatives you've considered
headaches and more coffee and continue with GREL snakes.

Additional context
Apache StringUtils seems to have already 2 nice functions to help replaceEach() and replaceEachRepeatedly() :
http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#replaceEach-java.lang.String-java.lang.String:A-java.lang.String:A-

... as well as many "index" methods.

Example Problem OpenRefine Project with provided "GREL snake" current solution:
vidal_santos.openrefine.tar.gz

Metadata

Metadata

Assignees

Labels

Type: Feature RequestIdentifies requests for new features or enhancements. These involve proposing new improvements.grelThe default expression language, GREL, could be improved in many ways!

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions