Skip to main content
gautams4173
Known Participant
May 2, 2023 10:01 AM UTC
Solved

Fetch Name from URL

Hi,

I have an URL of the format : https://www.google.com/<unique_id>_<fileName>_<remaining url>

Now my requirement is I want to fetch the filename out of the URL ... the first part will always remain the same  i.e.

first is https ... till .com/

then comes an unique identifier 

then followed by '_'

then followed by filename

then followed by '_' and the remaining URL 

Can anyone help me fetch the filename ? 

sanchitg0002
Inspiring
May 2, 2023 10:48 AM UTC

Try this

a!localVariables(
  local!url: "https://www.google.com/1234_xyz_pqr_abc.png?_",
  local!start: find("_", local!url),
  local!end: find("?", local!url),
  right(
    left(local!url, local!end - 1),
    local!end - local!start - 1
  )
)

18 replies

stewart.burchell
Employee
May 2, 2023 10:06 AM UTC

You can use fn!split() to break the url string up into an array of sub-strings. Assuming the rules you've provided hold true the <filename> will be in the 2nd item in the resulting array:

fn!split(ri!url, "_")[2]

gautams4173
Known Participant
May 2, 2023 10:17 AM UTC

The problem here is the filename can contain '_' .. so it will split based on that too 

stewart.burchell
Employee
May 2, 2023 10:31 AM UTC

Ok, so that was one of the "rules" in the problem statement that you'd neglected to mention. Are there other "rules" that might help guide you to a solution? For example, is the length and/or format of the value of "unique_id" deterministic? Will the initial part of the URL always end ".com" ? Anything predictable/deterministic about the value of the "remaining_url" ?

sanchitg0002
Inspiring
May 2, 2023 10:08 AM UTC

a!localVariables(
  local!url: "https://www.google.com/1234_xyz.png_",
  extract(local!url, "_", "_")
)

gautams4173
Known Participant
May 2, 2023 10:21 AM UTC

Hi,

The file name can contain '_'

stefanhelzle0001
Brainy
May 2, 2023 10:29 AM UTC

As we do not know what the remaining URL looks like, this becomes difficult.

Did you consider a regex expression?

tushark6475
Participating Frequently
May 2, 2023 1:40 PM UTC

Hi Gautam, below described logic will work for you.

a!localVariables(
  local!temp: split(
    "https://www.google.com/_testFile_Second_",
    "_"
  ),
  local!file: if(
    length(local!temp) > 3,
    joinarray(
      a!forEach(
        items: local!temp,
        expression: if(
          contains({ 1, count(local!temp) }, fv!index),
          null(),
          fv!item
        )
      ),
      "_"
    ),
    local!temp[2]
  ),
  local!file
)




gautams4173
Known Participant
May 2, 2023 2:56 PM UTC

Thanks [emoticon:33306c418930400bac28808410f8ac8b]

New Participant
Aug 19, 2026 5:53 PM UTC

You can extract the filename by identifying the text between the first underscore after the unique identifier and the next underscore. Since the URL structure is consistent, the filename will always be the portion between those two underscores.

mikes0011
Brainy
Aug 20, 2026 4:29 PM UTC

spam

The Expression Guru