How to replace lone dash symbols without modifying other cells using Regex

Hi knimefriends! We are working in a proyect but stucked in a regular expression. We are trying to replace this type of simbol “-” (dash / hyphen) but we can’t get the correct formula to make it work. Our main problem is that the database has this simbol alone and in other cases they are between other words, and we only have to replace the ones that are alone in a cell.

image

---------------------------------------- SPANISH TRANSLATION--------------------------------

Hola knimeamigos! Estamos trabajando en un proyecto pero nos estancamos en una expresión regular. Estamos tratando de reemplazar este tipo de símbolo “-” (guion / guion medio) pero no encontramos la formula correcta para que funcione. Nuestro problema principal es que nuestra base cuenta con casos en los que estos se encuentran entre otras palabras y nosotros queremos reemplazar aquellos casos en los que se encuentran solos en una celda.

(queremos pasar de esto (marcado en rojo) a esto (marcado en verde) sin modificar estos (marcados en azul)

  • => _

We tryed regexReplace($$CURRENTCOLUMNS$$, “-” , “_”) but doesn’t recognise the first term as a function (appears in green when it should be in red). We also tryed “\-” but the same thing happened.

In these cases, only no cape hero can save us… :sweat_smile:

Hi @Tripaflaca and welcome to the forum.

I am moving your topic to the main AP forum so it will get more visibility. We have several RegEx pros around here, and I am sure one of them will be able to find a fix for you in short order. Cheers! :slight_smile:

4 Likes

Hola @Tripaflaca y bienvenido al forum de la comunidad de KNIME !

Si lo que quieres obtener como resultado es lo siguiente:

image

Esto se puede hacer muy facilmente con un nodo -Rule Engine-

Y la instruccion siguiente:

$Numbers-Words$ = "-" => "_"
TRUE => $Numbers-Words$

No dudes en volver a preguntar si este no es el resultado esperado :wink:

Saludos
Ael

5 Likes

I had success with the String Manipulation (Multi Column) node using the expression:

regexReplace($$CURRENTCOLUMN$$,"^-$","_" )

  • ^ specifies that the matching string should be at the beginning of the line
  • $ specifies that the matching string should be at the end of the line
6 Likes

More examples of what you can do with Regex just in case:

4 Likes

While this can surely be done via Regex, basically "^-$" literally means “-” as the whole value, therefore a simple check if the column value = “-” is enough (and probably faster), like @aworker did :smiley:

$Numbers-Words$ = "-" => "_"
TRUE => $Numbers-Words$
3 Likes

Hello @bruno29a ,

Can this syntax be used on multiple columns at the same time ?
It seems they need to use it on multiple string columns.
I thought ‘if else’ can be used in string manipulations (multi column) but it gives me an error.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.