Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to modify Google Drive shared file URL

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?

like image 928
Mr.Syrax Avatar asked Apr 22 '14 16:04

Mr.Syrax


People also ask

Can Google Drive urls be changed?

The file name and the file's URL won't change and thus all the old shared links will now automatically point to the new version of your file.

How do I customize a drive link?

Open the file or folder in Google Drive. Click the “Share” button on the top right. In the “Share with People and Groups” box, enter the email addresses of the people you want to share the link with. Under “Get Link”, choose “Change” so you can set the appropriate permissions.

What is a good regex to match a URL?

@:%_\+~#= , to match the domain/sub domain name.

How do I find the URL of a file in Google Drive?

Open any file in Google Drive, click the Share button and you'll get a URL (link) that others can use to access your file.


2 Answers

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! :)

like image 80
Pedro Lobito Avatar answered Oct 27 '22 01:10

Pedro Lobito


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
like image 30
donfuxx Avatar answered Oct 27 '22 00:10

donfuxx