0

I want to get the file path by its firebase link. For example, the source url was "https://firebasestorage.googleapis.com/v0/b/dev.appspot.com/o/temp%2F1-5%2Ftemp_file_637605453170534484%2F2.png?alt=media&token=575e6226-1ffc-4697-b44a-724d2b7f734d"

I want to only get the temp%2F1-5%2Ftemp_file_637605453170534484%2F2.png (this part changes every time, it was not always a .png or temp at start) part. I know its possible with regex but I'm not familiar with regex and I cant find a key to compare. Note that the firebase url seems like to be constants on the start string so every url I have its start with https://firebasestorage.googleapis.com/v0/b/dev.appspot.com/o/

Rashid
  • 1,700
  • 1
  • 23
  • 56
  • 2
    You question lacks details: 1. Does the desired substring always start with `temp`? 2. Does it always end with `.png` or `?`, etc. – frankenapps Jun 29 '21 at 06:45
  • @frankenapps it wont since the file thats the file name and they allowed to upload any files – Rashid Jun 29 '21 at 06:54
  • 1
    Please show your attempted regex – anubhava Jun 29 '21 at 06:56
  • **Assuming** that the string you want is always at the end of the url (after the last `/`, i.e. it doesn't have any slashes in it) then it becomes quite easy. Try `[^\/]+$` this regex, which basically says _"match all (1 or more) non-slash characters at the end of the string"_ . Or just split it at all slashes and take the last part (that doesn't even use regex) – gdcodes Jun 29 '21 at 08:00
  • Edit : I found this existing SO post with answers to do what I suggested, if that works for you... It even has the same regex pattern i just commented, with other possibilities https://stackoverflow.com/questions/19776979/regex-get-all-characters-after-last-slash-in-url – gdcodes Jun 29 '21 at 08:05

1 Answers1

1

You can use a simple regex to group the base URL, File path, and the query parameters like this.

(https://firebasestorage.googleapis.com/v0/b/dev.appspot.com/o/)(.*)(\?.*)

Then you will get the complete URL as the full match and,

Capturing group 1 : https://firebasestorage.googleapis.com/v0/b/dev.appspot.com/o/

Capturing group 2 : temp%2F1-5%2Ftemp_file_637605453170534484%2F2.png

Capturing group 3 : ?alt=media&token=575e6226-1ffc-4697-b44a-724d2b7f734d

You can get the complete file path by extracting capturing group 2