Regex to modify Google Drive shared file URL

后端 未结 4 816
萌比男神i
萌比男神i 2021-02-04 16:37

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         


        
4条回答
  •  [愿得一人]
    2021-02-04 17:10

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

提交回复
热议问题