[CHALLENGE] Fun with RegEx

I’ve been trying to solve this one for a while and am coming up short. I even tried to use ChatGPT to give me some ideas, but no luck.

INPUT:
CopiaInvestor2022-12Update-BenchmarkLens.pdf

DESIRED RESULT:
Copia Investor 2022-12 Update - Benchmark Lens.pdf

Essentially, it boils down to this:

  1. Positive lookbehinds are NOT allowed.

  2. Has to work within Bubble’s “Find and Replace” function:
    image

  3. Lowercase followed by uppercase or number, insert space. (examples: “aB” becomes “a B”, “a2” becomes “a 2”.

  4. A bunch of uppercase characters followed by a lowercase letter, insert space between the last uppercase letter and 2nd to last uppercase character. (example: “ABCdef” becomes “AB Cdef”)

  5. Dash, insert spaces on both sides. EXCEPT when the dash is between numbers, do nothing. (examples: “a-b” becomes “a - b”, “2-2” doesn’t change, “a-2” becomes “a - 2”, “2-a” becomes “2 - a”)

  6. A bunch of numbers and/or dashes together, insert space on both sides. (examples: “a2022b” becomes “a 2022 b”, “a2022-12b” becomes “a 2022-12 b”, “a2022–12b” becomes “a 2022–12 b”)

Anyone up for a brain challenge and have any ideas?

Cheers!
Al

You’re almost certainly going to have to do this in multiple steps (which you already have very clear rules for). Get to know regexr.com I guess.

BTW, your original filename in your example is much better than your proposed destination, so why bother?

Sounds like a job for basic JavaScript to me!

3
image

4
image

5
image

6
image

image

1 Like

PSST… @dorilama likes regex, pass it on!

Though note that the URLencoded version of the result will be ugly, should you need to do that @ALB, because spaces.

1 Like

Who doesn’t love regex!?! :laughing:

1 Like

Love this, thank-you @dorilama!

I did originally try to run a few of these separately and string them together but couldn’t get it to work. Maybe I’m doing this wrong, but stringing these find/replaces together only runs the 1st one for me? Figured it was just a Bubble limitation or something.
image

A better way to do this?

Please note I was assuming I had to make this one ugly function!

@keith: I’m not trying to build a better file name. I’m trying to make a file name more visually appealing (especially if it wraps).

You need all 4 find & replace concatenated in the same expression.
Make sure you set up everything like the pictures and double check the regex.

As you can see from the last screenshot it works.

@ALB - fair enough (though you could also just truncate to some number of characters and append “…” and the file extension [if desired]). “Just a thought” as that goody-goody Mike would say. :wink:

As for getting unexpected results: It’s veeery easy to mistype these patterns. Make sure you’re not getting errors by running in debug mode or inspecting the console.

But :find & replace is chainable and should work just fine if all your expressions are valid AND if your algorithm does what you think it should do.

To debug further: Take a sample filename as arbitrary text in a text element and build the chain step by step:

arbitrary text:find & replace 1
arbitrary text:find & replace 1:find & replace 2
arbitrary text:find & replace 1:find & replace 2:find & replace 3

Compare each line to ensure what you think should happen is actually happening. Could be that the order of how things happen doesn’t yield exactly what you want for all or just some filenames…

1 Like

Yep, had them all in. Only the first one is triggering. The 2nd, 3rd, and 4th ones do not ever get applied.

#1

#2

#3

#4

Showing original and all 4 iterations separately:
image

All 4 results are always the same:
image

image

image

Use a regex pattern must be enabled

Noooo!
Argh. So dumb. Thanks for pointing that out. smacks forehead

2 Likes

This topic was automatically closed after 70 days. New replies are no longer allowed.