正则表达式来修改Google云端硬盘共享文件的URL [英] Regex to modify Google Drive shared file URL
问题描述
我正在尝试创建一个脚本来将常规的Google云端硬盘共享网址转换为直接下载网址。原始网址如下所示:
https://drive.google.com/file/d/FILE_ID/edit?usp =分享
,需要转换成如下所示:
https://drive.google.com/uc?export=download&id=FILE_ID
所以我试图让我的正则表达式,我不是非常有经验,抓住需要删除/改变的文本。我使用RegExr来尝试创建它,但我只得到
/ file / d /
我尝试了一种负面预测,但似乎无法正常工作。任何建议?
$ b 适用于PHP: $ b 对于Python: Perl: 适用于Java: 对于Ruby:
$ link = preg_replace('%https://drive\.google\.com/file/d/(。*?)/.*?\?usp=sharing%','https:// drive。 google.com/uc?export=download&id=$1',$ link);
result = re.sub(rhttps://drive\.google\.com/file/d/(。*?)/.*?\ ?usp = sharing,rhttps://drive.google.com/uc?export=download&id=\1,url)
$ subject =〜小号的https://drive\.google\.com/file/d/ /.*?\?usp=sharing!https://drive.google.com/uc?export=(*?)下载和放大器;!ID = $1克;
字符串resultString = subjectString.replaceAll(https://drive\\.google\\.com/file/d/(。*?)/。 *?\\?usp = sharing,https://drive.google.com/uc?export=download&id=$1);
result = subject.gsub(/https:\/\/drive\.google\.com\/file\/d\/(。 *?)\ /.*?\?usp = sharing /,'https://drive.google.com/uc?export=download&id=\1')
$ c $对于C#:
<$>
c $ 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} $ subjecthttps://drive.google.com/uc?export=download\\&id=\\ 1result
$对于Oracle:
结果为p $ p>
:= REGEXP_REPLACE(主题,'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)
$ c $对于Delphi XE:code> 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:replac e($ input,https://drive\.google\.com/file/d/(。*?)/.*?\?usp=sharing,https://drive.google。 com / uc?export = download& amp; amp; id = $ 1)
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)
如果您需要不同的语言,请告诉我! :)
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?
解决方案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! :)
这篇关于正则表达式来修改Google云端硬盘共享文件的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!