How do I remove words from a string if they appear in an array?

Here is my scenario. I want to get a file name from Google Drive and check it against a list of words. If one or more of those words appears in the list, I’d like remove them and update the filename.

I’ve considered splitting the filename string into an array itself and then iterating and looping a manual check of each word in array-1 against each word in array-2…but array-2 (the word list) has 75 words in it, so it would get operationally expensive VERY quickly. I feel like there’s go to be a better way.

Thanks in advance for your help!

Have you tried the replace() function with a regular expression?

2 Likes

Welcome to the Make community!

When asking for help with creating a regex pattern for a text parser module, I strongly suggest you do not censor the text that you actually need to match.

Please also do not share screenshots of text (even partial text). Instead, copy and paste the text here as well so that we can run it against test patterns.

If you do not provide proper examples, you could be wasting our time as we have to guess your sample input. Not only that, you may not get the correct answer, or it may take several “guesses”.

Help us help you.

1. Screenshots of module fields and filters

Please share screenshots of relevant module fields and filters in question? It would really help other community members to see what you’re looking at.

You can upload images here using the Upload icon in the text editor:
Screenshot_2023-10-07_111039

2. Scenario blueprint

Please export the scenario blueprint file to allow others to view the mappings and settings. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.

Screenshot_2023-08-24_230826
(Note: Exporting your scenario will not include private information or keys to your connections)

Uploading it here will look like this:

blueprint.json (12.3 KB)

3. And most importantly, Output bundles

Please provide the output bundles of the modules by running the scenario, then click the white speech bubble on the top-right of each module and select “Download output bundles”.
Screenshot_2023-10-06_141025

A.

Save the bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

Uploading it here will look like this:

bundle.txt (12.3 KB)

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted output bundle in this manner:

  • Either add three backticks ``` before and after the code, like this:

    ```
    input/output bundle content goes here
    ```

  • Or use the format code button in the editor:
    Screenshot_2023-10-02_191027

Providing the output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

Following these steps will allow others to assist you here. Thanks!

1 Like

I’m not great with regex. Is it possible to create a single pattern that encompasses the entire word list? Or how are you thinking?

Thanks!

Thanks for the guidance. Here’s the word list you requested. I’m not 100% sure a regex pattern is the solution here but I’m happy to be told otherwise as it’s not my strongest area.

a, about, actually, almost, also, although, always, am, an, and, any, are, as, at, be, became, become, but, by, can, could, did, do, does, each, either, else, for, from, had, has, have, hence, how, i, if, in, is, it, its, just, may, maybe, me, might, mine, must, my, neither, nor, not, of, oh, ok, when, where, whereas, wherever, whenever, whether, which, while, who, whom, whoever, whose, why, will, with, within, without, would, yes, yet, you, your

An important point also is that I need to make sure not to remove partial matches, like accidentally removing all of the letter A’s from every word, etc.

Do you have some example filenames too?

1 Like

Yup here’s the regular expression:

/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/gi

Explanation:

  • / and / are the delimiters for the regular expression pattern.
  • \b is a word boundary that ensures the pattern matches whole words and not substrings within larger words.
  • (a|about|actually|almost|...|your) is an alternation group that matches any of the words in the list.
  • \b is another word boundary at the end of the pattern.
  • g is a flag that makes the search global, meaning it will replace all occurrences of the matched words.
  • i is a flag that makes the search case-insensitive.

you can put that as an argument to the replace() function.

something like this

{{replace("this Without about test"; "/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/gi"; emptystring)}}

EDIT: looks like both the /g and /i flags CANNOT be used together in the replace() function. You can only use /g or /i but not /gi together. See below

6 Likes

Awesome thanks all!

@alex.newpath - I’ll give this a shot and thank you for the detailed explanation.

@samliew - to answer your question about example filenames, it’s hard because there really isn’t anything standard or consistent with them. They could be 2 words or 8, include a number or not, be real words or fake.

2 Likes

Ok so after hitting my head against a wall for 2 hours, I learned two things.

  1. the replace() does not perform well with regex until you fully save changes in the scenario.

  2. the pattern you provided isn’t working for me with both (‘g’ and ‘i’) included at once within the replace() function. If I remove one of them, the function works as you’d expect (minus the respect feature of the flag removed). But the moment I add the second flag back in, the whole pattern ceases to work at all. It doesn’t produce an error, it just fails to match anything.

I did a little come across your old post here that seems to indicate there is some limitation behind the scenes, but also states you can use both those flags together.

Any thoughts?

As a lot of stuff in Make there is some undocumented stuff. Looks like both flags cannot be used together.

{{replace(lower("this Without about test"); "/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/g"; emptystring)}}

will work since the input text is lowercased first and all the words on the RegEx are also lower cased. Thanks @samliew for the correction to work around the inability to place both g and i modifiers into the expression.

You may want to use the Text Parser module also if you don’t want to do the workaround approach above.

Copy and paste this scenario snippet into your scenario canvas

This text will be hidden{
“subflows”: [
{
“flow”: [
{
“id”: 1,
“module”: “util:SetVariables”,
“version”: 1,
“parameters”: {},
“mapper”: {
“variables”: [
{
“name”: “test”,
“value”: “{{replace(lower("this Without about test"); "/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/g"; emptystring)}}”
}
],
“scope”: “roundtrip”
},
“metadata”: {
“designer”: {
“x”: 0,
“y”: 0
},
“restore”: {
“expect”: {
“variables”: {
“items”: [
null
]
},
“scope”: {
“label”: “One cycle”
}
}
},
“expect”: [
{
“name”: “variables”,
“type”: “array”,
“label”: “Variables”,
“spec”: [
{
“name”: “name”,
“label”: “Variable name”,
“type”: “text”,
“required”: true
},
{
“name”: “value”,
“label”: “Variable value”,
“type”: “any”
}
]
},
{
“name”: “scope”,
“type”: “select”,
“label”: “Variable lifetime”,
“required”: true,
“validate”: {
“enum”: [
“roundtrip”,
“execution”
]
}
}
],
“interface”: [
{
“name”: “test”,
“label”: “test”,
“type”: “any”
}
]
}
},
{
“id”: 2,
“module”: “regexp:Replace”,
“version”: 1,
“parameters”: {},
“mapper”: {
“pattern”: “\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b”,
“value”: “{{emptystring}}”,
“global”: true,
“sensitive”: false,
“multiline”: false,
“singleline”: false,
“text”: “this Without about test”
},
“metadata”: {
“designer”: {
“x”: 300,
“y”: 0,
“messages”: [
{
“category”: “last”,
“severity”: “warning”,
“message”: “A transformer should not be the last module in the route.”
}
]
},
“restore”: {
“expect”: {
“global”: {
“mode”: “chose”
},
“sensitive”: {
“mode”: “chose”
},
“multiline”: {
“mode”: “chose”
},
“singleline”: {
“mode”: “chose”
}
}
},
“expect”: [
{
“name”: “pattern”,
“type”: “text”,
“label”: “Pattern”,
“required”: true
},
{
“name”: “value”,
“type”: “text”,
“label”: “New value”
},
{
“name”: “global”,
“type”: “boolean”,
“label”: “Global match”,
“required”: true
},
{
“name”: “sensitive”,
“type”: “boolean”,
“label”: “Case sensitive”,
“required”: true
},
{
“name”: “multiline”,
“type”: “boolean”,
“label”: “Multiline”,
“required”: true
},
{
“name”: “singleline”,
“type”: “boolean”,
“label”: “Singleline”,
“required”: true
},
{
“name”: “text”,
“type”: “text”,
“label”: “Text”
}
]
}
}
]
}
],
“metadata”: {
“version”: 1
}
}

1 Like

I thought this is already a known issue, I’ve even reported it to Make support a year ago.

The workaround is lowercase the input text and then have the pattern in lowercase as well. Then only use the global flag.

samliewrequest private consultation

Join the unofficial Make Discord server to chat with us!

2 Likes

Yeah and I reported an obvious bug in the average() function which they told me to workaround ignoring the fact that others may hit it and the workaround is not documented anywhere other than in this post.

2 Likes

Closing the loop on this thread.

@alex.newpath’s initial suggestion got most of the way there except for the later referenced bug that won’t allow both the /g and /i flags to be used together.

Wrapping the input text in a lower() function works for my use case. It should be noted for others though that your output will now be in all lowercase and need to be modified again to match your desired format.

Two additional bugs appeared in my testing

  1. Whitespace would appear at the front and end of a string if the first/last word was replaced. To solve for this, I wrapped the entire command in a trim() function.

  2. Double spaces would appear if two words next to each other were replaced with an empty string. To solve for this I wrapped the entire command (including the new trim() function) in a 2nd replace() with /\s{2,}/ as the expression and replaced it with a single space

Here’s my final result.

{{replace(trim(replace(lower(response.payload.`input_name`); "/\b(a|about|actually|almost|also|although|always|am|an|and|any|are|as|at|be|became|become|but|by|can|could|did|do|does|each|either|else|for|from|had|has|have|hence|how|i|if|in|is|it|its|just|may|maybe|me|might|mine|must|my|neither|nor|not|of|oh|ok|when|where|whereas|wherever|whenever|whether|which|while|who|whom|whoever|whose|why|will|with|within|without|would|yes|yet|you|your)\b/g"; emptystring)); "/\s{2,}/"; space)}}

Thanks to @alex.newpath, @samliew, and everyone who helped here. :crossed_fingers: Make either fixes the multiple flags bug, or at least documents the limitation better!

4 Likes