11

I'm trying to create a script to convert a regular google drive share URL to a direct download URL. The raw URL looks like this:

https://drive.google.com/file/d/FILE_ID/edit?usp=sharing

and needs to be converted to look like this:

https://drive.google.com/uc?export=download&id=FILE_ID

So I'm trying to make my regex, which I'm not very experienced with, to grab the needed text to be deleted/changed. I'm using RegExr to try to create it, but I only get as far as

/file/d/

I've tried a negative lookahead, but it doesn't seem to work. Any suggestions?

donfuxx
  • 11,277
  • 6
  • 44
  • 76
Mr.Syrax
  • 396
  • 1
  • 9
  • 26

4 Answers4

30

UPDATED ON 23 March 2017


For PHP:

$link = preg_replace('%https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing%', 'https://drive.google.com/uc?export=download&id=$1', $link);

For Python:

result = re.sub(r"https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing", r"https://drive.google.com/uc?export=download&id=\1", url)

For Perl:

$subject =~ s!https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing!https://drive.google.com/uc?export=download&id=$1!g;

For Java:

String resultString = subjectString.replaceAll("https://drive\\.google\\.com/file/d/(.*?)/.*?\\?usp=sharing", "https://drive.google.com/uc?export=download&id=$1");

For Ruby:

result = subject.gsub(/https:\/\/drive\.google\.com\/file\/d\/(.*?)\/.*?\?usp=sharing/, 'https://drive.google.com/uc?export=download&id=\1')

For C#:

resultString = Regex.Replace(subjectString, @"https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing", "https://drive.google.com/uc?export=download&id=$1");

For R Language:

~gsub("https://drive\\.google\\.com/file/d/(.*?)/.*?\\?usp=sharing", "https://drive.google.com/uc?export=download&id=\\1", subject, perl=TRUE);

For Javascript:

result = subject.replace(/https:\/\/drive\.google\.com\/file\/d\/(.*?)\/.*?\?usp=sharing/g, "https://drive.google.com/uc?export=download&id=$1");

For TCL:

regsub -linestop -all {https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing} $subject "https://drive.google.com/uc?export=download\\&id=\\1" result

for Oracle:

result := REGEXP_REPLACE(subject, 'https://drive\.google\.com/file/d/(.*)/.*?\?usp=sharing', 'https://drive.google.com/uc?export=download&id=\1', 1, 0, 'c');

For C++:

wxString ;
wxRegEx regexObj(_T("(?p)\\Ahttps://drive\\.google\\.com/file/d/(.*?)/.*?\\?usp=sharing"), wxRE_ADVANCED);
regexObj.ReplaceAll(&subjectString, _T("https://drive.google.com/uc?export=download\\&id=\\1"));

For Groovy:

Matcher regexMatcher = subjectString =~ /https:\/\/drive\.google\.com\/file\/d\/(.*?)\/.*?\?usp=sharing/
String resultString = regexMatcher.replaceAll('https://drive.google.com/uc?export=download&id=$1');

For PostgreSQL:

SELECT REGEXP_REPLACE(mycolumn, $$(?p)https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing$$, $$https://drive.google.com/uc?export=download&id=\1$$, 'g') FROM mytable;

For VisualBasic.NET:

Dim RegexObj As New Regex("https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing")
ResultString = RegexObj.Replace(SubjectString, "https://drive.google.com/uc?export=download&id=$1")

For Delphi XE:

Dim RegexObj As New Regex("https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing")
ResultString = RegexObj.Replace(SubjectString, "https://drive.google.com/uc?export=download&id=$1")

For PowerShell:

$regex = [regex] 'https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing'
$result = $regex.Replace($subject, 'https://drive.google.com/uc?export=download&id=$1')

For Xpath:

fn:replace($input, "https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing", "https://drive.google.com/uc?export=download&id=$1")

For VBscript:

Dim myRegExp, ResultString
Set myRegExp = New RegExp
myRegExp.Global = True
myRegExp.Pattern = "https://drive\.google\.com/file/d/(.*?)/.*?\?usp=sharing"
ResultString = myRegExp.Replace(SubjectString, "https://drive.google.com/uc?export=download&id=$1")

If you need a different language just let me know! :)

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 4
    You forgot Ruby, R, Logo and C# :) – aliteralmind Apr 22 '14 at 16:23
  • 2
    I don't see no Logo!! ;) – aliteralmind Apr 22 '14 at 16:34
  • 3
    +1 for multi-language-roundhouse-kick answer :-D But will we ever figure out which language was requested.. – donfuxx Apr 22 '14 at 16:36
  • @donfuxx I guess not! – Pedro Lobito Apr 22 '14 at 16:38
  • 3
    +1. Now just add Basic, Pascal, ALGOL, COBOL, Excel, Fortran, Go!, and Assembly, and Logo, and I'll mail a bouquet to the postal address of your choice. – aliteralmind Apr 22 '14 at 16:40
  • 3
    @aliteralmin I will !!! But please don't send a bouquet, Ill get in trouble with my wife...send a bottle of wine! :))))) – Pedro Lobito Apr 22 '14 at 16:47
  • 3
    I'll send the wine and then the flowers a few hours after that. Make sure she's drunk when they arrive. – aliteralmind Apr 22 '14 at 17:39
  • @Tuga: As did you! Awesome answer! – aliteralmind Apr 22 '14 at 17:44
  • 3
    @aliteralmind I was very surprised...for not receiving the flowers ;) – Pedro Lobito Apr 25 '14 at 14:35
  • Awesome!! Can i have it in prolog? :P – Victor Henriquez Apr 30 '14 at 10:33
  • The PHP version is not working anymore unfortunately. Maybe the URLs have updated since this was posted. – Henrik Petterson Mar 23 '17 at 15:42
  • @HenrikPetterson Part of the sharing link changed from `edit` to `view`. I've updated **all regex versions**, including `php`! Check it out http://ideone.com/GAY9Z2 – Pedro Lobito Mar 23 '17 at 21:26
  • @PedroLobito Actually, this is still incorrect. The `drive` part should be `docs` and so on. I can provide you with an example link if you want. – Henrik Petterson Mar 24 '17 at 08:42
  • That would alter the original answer. I've tested the regex with files from google drive at it works as expected. Please create a new question and I'll be happy to help you. – Pedro Lobito Mar 24 '17 at 10:32
  • It is because the shared URL has been updated since this question was posted. You can confirm this in one minute by clicking the Share button in Google Drive and then `get shareable link`. You will get something like this: `https://docs.google.com/document/d/1eeVLgzhZD7z4MfN9M1XKQOzz0A41ya2QicVEP1OsoQ/edit?usp=sharing` -- as you can see, there are some changes to the structure of the regex. I adjusted your code and the following works: `%https://docs\.google\.com/document/d/(.*?)/edit\?usp=sharing%` – Henrik Petterson Mar 24 '17 at 11:47
  • This only happens if you're inside google **docs** not google drive itself https://drive.google.com/drive/my-drive .As I said previously, please open a new question, I'll help you. – Pedro Lobito Mar 24 '17 at 12:00
6

You don't need regex for that you can complete the url transformation with 2 chained string replace. See for example this (in Java) :

String url="https://drive.google.com/file/d/FILE_ID/edit?usp=sharing";
url = url.replace("/file/d/", "/uc?export=download&id=").replace("/edit?usp=sharing", "");
System.out.print(url); 

==> The output:

https://drive.google.com/uc?export=download&id=FILE_ID
donfuxx
  • 11,277
  • 6
  • 44
  • 76
3

This works in PCRE, in regex101:

  • Find what: ^(https:\/\/drive\.google\.com\/)file\/d\/([^\/]+)\/.*$
  • Replace with: $1uc?export=download&id=$2
  • Try it: http://regex101.com/r/dX1zD2

Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
0

You can extract the ID with this regex:

regex = "([\w-]){33}|([\w-]){19}"
match = re.search(regex,url)

33 chars are for normal drives and 19 chars are for team drives After that you can put the extracted ID in whatever formatted url you make