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 ?
Discussion posts and replies are publicly visible
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]
a!localVariables( local!url: "https://www.google.com/1234_xyz.png_<remaining url>", extract(local!url, "_", "_") )
The problem here is the filename can contain '_' .. so it will split based on that too
The file name can contain '_'
As we do not know what the remaining URL looks like, this becomes difficult.
Did you consider a regex expression?
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" ?
Ok the rules would be :
The url will always start with https://www.google.com/
The unique Id will always be 36 digits
The unique ID will be followed by an '_',
the underscore will be followed by the file name (file name can contain underscores )
file name will be followed by '?' which marks the end of file name
then it will be followed by the rest of the url whose length is uncertain and can contain all kinds of characters
I will look out for that now
Try this
a!localVariables( local!url: "https://www.google.com/1234_xyz_pqr_abc.png?_<remaining url>", local!start: find("_", local!url), local!end: find("?", local!url), right( left(local!url, local!end - 1), local!end - local!start - 1 ) )
Ok, so we can say the first part of the URL will always be 60 characters long (the length of the initial part of the URL plus the 36 characters of the unique Id).
So we can extract the url after all of this "prefix". That will leave us with the filename and the trailing part of the URL. We also know that the filename is terminated by the "?" character so we can split the remaining string at this point:
a!localVariables( local!fileNameStartsAtPosition: 60, /* in reality you'd make this a constant */ local!fileNameAndRemainingUrl: fn!right( ri!url, fn!len(ri!url) - local!fileNameStartsAtPosition ), fn!split(local!fileNameAndRemainingUrl, "?")[1] )