正则表达式修改Google云端硬盘共享文件网址

时间:2014-04-22 16:02:39

标签: regex url google-drive-api

我正在尝试创建一个脚本,将常规的Google云端硬盘共享网址转换为直接下载网址。原始URL如下所示:

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

并且需要转换为如下所示:

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

因此,我正在尝试使用我不熟悉的正则表达式来获取要删除/更改的所需文本。我正在使用RegExr尝试创建它,但我只能达到

/file/d/

我尝试过一种消极的前瞻,但它似乎没有用。有什么建议吗?

4 个答案:

答案 0 :(得分:23)

2017年3月23日更新


对于PHP:

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

对于Python:

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

对于Perl:

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

对于Java:

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

对于Ruby:

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

对于C#:

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

对于R语言:

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

适用于Javascript:

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

对于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');

对于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"));

对于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');

对于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;

对于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")

对于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")

适用于PowerShell:

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

对于Xpath:

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

对于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")

如果您需要其他语言,请告诉我们! :)

答案 1 :(得分:6)

不需要正则表达式,因为您可以使用2个链式字符串替换来完成网址转换。例如,参见(在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); 

<强> ==&GT;输出:

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

答案 2 :(得分:3)

这适用于PCRE,在regex101中:

  • 找到:^(https:\/\/drive\.google\.com\/)file\/d\/([^\/]+)\/.*$
  • 替换为:$1uc?export=download&id=$2
  • 试一试:http://regex101.com/r/dX1zD2

请考虑将Stack Overflow Regular Expressions FAQ加入书签以供将来参考。

答案 3 :(得分:0)

您可以使用此正则表达式提取ID:

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

33个字符用于普通驱动器,19个字符用于团队驱动器 之后,您可以将提取的ID放入您制作的任何格式的网址中