String Replacer with Regex not working

Hey everyone,
So, these words " in " , " for ", " hand " & " cars " along with space at the beginning and end are substrings to be replaced with a single space in a string

Here’s the regex, I used in the string replacer node using or (|)

But all the replacement are not done.
replacement
Col 1: Actual String to match in
Col 2 : Replaced Column

What am I doing wrong in this string replacer node regex configuration?

Hi @Ummehaani , this is probably because of the spaces you are adding, it will find (and replace) only 1 of the the list. I understand why you want the spaces included, but I don’t know how to do this in Regex - and it looks like neither do you :slight_smile:

In that case, why not just use regular replace() via String Manipulation like this?

replace(replace(replace(replace($Keyword$
  , " in ", " ")
  , " for ", " ")
  , " hand ", " ")
  , " cars ", " ")

Results:
image

5 Likes

Hi @Ummehaani, I think @bruno29a is right that part of the problem you had is because of the spaces. The string replacer is trying to find a word wrapped with spaces, but having also replaced the spaces, it then doesn’t find a neighbouring word that is also wrapped with spaces.

You could try using regex “word boundaries” instead which are represented by \b

\bcars\b|\bin\b|\bhand\b|\bfor\b

This would find your keywords whenever they are separate words, and not just when surrounded by spaces.

That doesn’t then remove the surrounding spaces though, if present, so you might have to follow it with a further String Replacer whose job it is to replace two spaces with one space.

Alternatively, @bruno29a’s solution works well too as a single node, and might be more straightforward

7 Likes

This is probably a workaround to your RegEx that I wouldn’t use in production but I tried your RegEx with one modification that worked:

The results matched your goal, I think:

image

Hi @lenexa_jayhawk , this probably works for these specific text, but probably won’t work with other texts.

One of the reasons I added the “handsome cars” was to show that the “hand” part from “handsome” would not change, and it still works here with your "|hand " rule simply because “handsome” qualifies, but I think the original idea was to look for “hand” as a word, or “cars”, or “in” etc as words.

So, in your case, if you have a text like “within cars”, the "within " will be changed to “with” because incidently, "within " will match "in ". Similarly if you have a text like “forehand cars”, the “forehand” will be changed to “fore”. So this will not match the strings as words, but rather as substrings.

Hi @lenexa_jayhawk , And here are the results I was mentioning above.

With my original solution:
image

With what you are suggesting:
image

That’s why @takbb actually tried using the \b in the Regex, because that’s what would make sure that these strings are matched as words.

1 Like

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