Tools -> Switch -> Pattern -> Cases -> matches?

I have an array of long text that either has a “Hourly Rate: $a-$b” or “Budget: $c” within.

The documentation of Tools > Switch doesn’t cover the “Use regular expressions to match = Yes” scenario, so I’m trying to find out how to get the regEx matches here.

For each case of the Tools > Switch, I want to further process the case’s regEx’s matches, that is:

  • for the “Hourly Rate” case, set output to b
  • for the “Budget” case, set output to c

source data.txt (205.5 KB)

scenario.json (216.2 KB)

Welcome to the Make community!

Remove

`                         `s

You shouldn’t need the regex delimiter, similarly to the Text Parser pattern field.

Plus I believe you need to escape the backslash in the break tag.

If you need further assistance, please provide the following:

1. 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.


(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)

2. And most importantly, Input/Output bundles

Please provide the input and output bundles of the trigger/iterator/aggregator modules by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A.

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

Uploading them here will look like this:

module-1-output-bundle.txt (12.3 KB)

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles 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:

Providing the input/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!

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

Backticks avoid needing to escape the backslash. It works on regEx101.com (ECMAScript selected).

However, if needed I can remove the backticks and escape the backslash. This will return us to my question:

In the absence of documentation, how do I get the matched text per case (as illustrated with the “//“ in the screenshot), so I can further use functions to get b or c as output?

Welcome to the Make community!

You can use a Text Parser “Match Pattern” module with this Pattern (regular expression):

<b>(?:Hourly Range|Budget.*?)[^:]+:\s+(?<rate>\$\d+(?:\.\d{2})?(?:\s*-\s*\$\d+(?:\.\d{2})?)?)

Proof

https://regex101.com/r/9PBgx9

Screenshot

Output


For more information, see Text Parser in the Make Help Center:

Match Pattern
The Match pattern module enables you to find and extract string elements matching a search pattern from a given text. The search pattern is a regular expression (aka regex or regexp), which is a sequence of characters in which each character is either a metacharacter, having a special meaning, or a regular character that has a literal meaning.

Hope this helps!

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

The case of either Hourly or Budget is important. That is, I need to know, either:

  • Hourly AND b
  • Budget AND c

Also, your suggestion is incorrect as it only returns the first digit of a and c, whereas I need b and c entirely — “10.00” or “100.00”.

I will be branching (with Router) directly after, processing Hourly differently to Budget.

Sorry, I didn’t realise some of them has commas, so here’s the updated regex pattern:

<b>(?<type>Hourly Range|Budget)[^:]+:\s+(?<rate>\$\d+(?:[.,]\d{2,3})*(?:\s*-\s*\$\d+(?:[.,]\d{2,3})*)?)

This will also extract the type, hourly or budget.

Proof: https://regex101.com/r/9PBgx9

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

So you’re effectively saying there’s no access to the captured values in the Switch Cases. The “use regular expressions” flag is simply a binary test — if [case pattern] has match, then [case output].

This isn’t as useful as implied by the UI.

Returning then to your updated regEx suggestion. It’s correctly capturing Budget and c, but Hourly is capturing $a-$b not b. This is incorrect. I need b ("100.00”) only.

Ok, I saw your first screenshot and thought that’s what you wanted.

1 Like

That screenshot illustrates the output of the regEx at that case. I asked then how to access the matches, so I can further grab b using functions to calculate the case’s output.

However, your suggested approach is entirely different (Text), so the screenshot (Switch & Cases) no longer applies.

The requirement hasn’t changed: when Hourly I still need b.

You don’t need a switch module, when a simple built-in if statement would work.

Here is the updated regex to exclude the dollar signs

<b>(?<type>Hourly Range|Budget)[^:]+:\s+\$(?<rate1>\d+(?:[.,]\d{2,3})*)(?:\s*-\s*\$(?<rate2>\d+(?:[.,]\d{2,3})*))?

Proof: https://regex101.com/r/9PBgx9

and here is the module export for you to paste into your scenario:

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

View Module Export Code

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 125,
                    "module": "regexp:Parser",
                    "version": 1,
                    "parameters": {
                        "pattern": "<b>(?<type>Hourly Range|Budget)[^:]+:\\s+\\$(?<rate1>\\d+(?:[.,]\\d{2,3})*)(?:\\s*-\\s*\\$(?<rate2>\\d+(?:[.,]\\d{2,3})*))?",
                        "global": false,
                        "sensitive": true,
                        "multiline": false,
                        "singleline": false,
                        "continueWhenNoRes": false
                    },
                    "mapper": {
                        "text": "{{124.text}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -1810,
                            "y": -254
                        },
                        "restore": {},
                        "parameters": [
                            {
                                "name": "pattern",
                                "type": "text",
                                "label": "Pattern",
                                "required": true
                            },
                            {
                                "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": "continueWhenNoRes",
                                "type": "boolean",
                                "label": "Continue the execution of the route even if the module finds no matches",
                                "required": true
                            }
                        ],
                        "expect": [
                            {
                                "name": "text",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "interface": [
                            {
                                "type": "text",
                                "name": "type",
                                "label": "type"
                            },
                            {
                                "type": "text",
                                "name": "rate1",
                                "label": "rate1"
                            },
                            {
                                "type": "text",
                                "name": "rate2",
                                "label": "rate2"
                            }
                        ]
                    }
                },
                {
                    "id": 128,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "value",
                        "scope": "roundtrip",
                        "value": "{{if(125.type = \"Budget\"; 125.rate2; 125.rate1 + \"-\" + 125.rate2)}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -1564,
                            "y": -255
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "value",
                                "label": "value",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

The Switch tool doesn’t do what I thought it does, so there is only the Text approach. I have no problem with that.

I also see that your updated regEx is capturing a|c and b separately now. That’s great.

However, your suggested regEx returns 22 matches (for the entire JSON), whereas there’s 30 items in the array. That is, it’s dropped 8 incorrectly. This would have been supported by the Switch’s Else.

Which ones are dropped? Perhaps those didn’t contain “Budget” or “Hourly”

Feel free to double check it,

Proof: https://regex101.com/r/PZFXXW

As you can see, there are only 22 matches with “Budget” or “Hourly”.

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

The negative edge cases are:

  1. Hourly with no range (neither a nor b) eg “Hourly:” → Hourly, b = null [NOTE: “Rate” is omitted]
  2. Hourly with a one rate only eg “Hourly Rate: $50.00” → Hourly, b = 50.00
  3. Neither Hourly nor Budget found in the input → “Not Specified"

Negative cases must still be processed via the Router (Hourly, Budget, Not Specified).

It’s a RegEx. It will only capture what matches. I expected the Switch approach to handle these negative edge cases, but it doesn’t. So my issue is your Text and Set Variable approach doesn’t support 30 input → 30 output — it drops 8.

In that case, you can toggle this to Yes

Screenshot_2024-06-13_220622

And then add a fallback in the next module like this

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

View Module Export Code

JSON

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 125,
                    "module": "regexp:Parser",
                    "version": 1,
                    "parameters": {
                        "pattern": "<b>(?<type>Hourly Range|Budget)[^:]+:\\s+\\$(?<rate1>\\d+(?:[.,]\\d{2,3})*)(?:\\s*-\\s*\\$(?<rate2>\\d+(?:[.,]\\d{2,3})*))?",
                        "global": false,
                        "sensitive": true,
                        "multiline": false,
                        "singleline": false,
                        "continueWhenNoRes": true
                    },
                    "mapper": {
                        "text": "{{123.description}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -2054,
                            "y": -248,
                            "messages": [
                                {
                                    "category": "link",
                                    "severity": "warning",
                                    "message": "The module is not connected to the data flow."
                                },
                                {
                                    "category": "reference",
                                    "severity": "warning",
                                    "message": "Referenced module 'JSON - Parse JSON' [123] is not accessible."
                                }
                            ]
                        },
                        "restore": {},
                        "parameters": [
                            {
                                "name": "pattern",
                                "type": "text",
                                "label": "Pattern",
                                "required": true
                            },
                            {
                                "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": "continueWhenNoRes",
                                "type": "boolean",
                                "label": "Continue the execution of the route even if the module finds no matches",
                                "required": true
                            }
                        ],
                        "expect": [
                            {
                                "name": "text",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "interface": [
                            {
                                "type": "text",
                                "name": "type",
                                "label": "type"
                            },
                            {
                                "type": "text",
                                "name": "rate1",
                                "label": "rate1"
                            },
                            {
                                "type": "text",
                                "name": "rate2",
                                "label": "rate2"
                            }
                        ]
                    }
                },
                {
                    "id": 128,
                    "module": "util:SetVariable2",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "name": "value",
                        "scope": "roundtrip",
                        "value": "{{if(125.type = \"Budget\"; 125.rate1; if(125.type = \"Hourly Range\"; 125.rate1 + \"-\" + 125.rate2; \"Not Specified\"))}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": -1809,
                            "y": -249
                        },
                        "restore": {
                            "expect": {
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "name",
                                "type": "text",
                                "label": "Variable name",
                                "required": true
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            },
                            {
                                "name": "value",
                                "type": "any",
                                "label": "Variable value"
                            }
                        ],
                        "interface": [
                            {
                                "name": "value",
                                "label": "value",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

Here are your 30 bundles, the last being a “Not Specified”.

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

If I understand correctly, you’re solving the 3. Not Specified edge case only here. OK.

But these are just as valid and more likely:

  1. Hourly with no range (neither a nor b) eg “Hourly” → Hourly, b = null [NOTE: “Rate” is omitted]
  2. Hourly with a one rate only eg “Hourly Rate: $50.00” → Hourly, b = 50.00

Does Set Variable need a switch() function akin to what I incorrectly expected the Switch Tool to do? That is, will the RegEx work for Hourly’s edge cases?

I’ve figured out how to use the switch module :slight_smile:

You can verify this using the bundles 22, 23, 24:

And edge cases Hourly or Budget found, but no value:

Hint: You must use six built-in functions with the switch module to achieve what you’re asking for.

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

Hmm…. Could you include the JSON for the Switch module. I worked out how to use the suggestion earlier to treat all cases already. But I’m interested in how readable the preferred Switch solution