文本编辑器(Sublime Text,Geany,Notepad ++等),用于从URL字符串中删除除一个参数值以外的所有参数 [英] Text Editor(Sublime Text, Geany, Notepad++ etc.) Regex to remove all parameters from URL string except one parameter-value
问题描述
我对Regex中的高级匹配模式不是很熟悉.
I am not very familiar with advanced matching patterns in Regex.
我有一些Google搜索URL,需要清理它们,而无需按住Backspace键5秒钟即可从URL中删除不必要的参数.
I have some Google Search URLs which I need to clean up without having to hold Backspace key for 5 seconds to remove unnecessary parameters from the URL.
比方说我有这个URL(下面的模式可以有许多不同的URL):
Let's say I have this URL(could many different URLs following patterns like below):
https://www.google.com/search?source=hp&ei=Ne4pXpSIHIW_9QOD-rmADw&q=laravel+crud+generator&oq=laravel+crud+generator&gs_l=psy-ab.3..0l8.1294.6845..7289...1.0..0.307.3888.0j20j2j1......0....1..gws-wiz.....6..0i131j0i362i308i154i357.PwlZ_932pXo&ved=0ahUKEwjU9pz4tJrnAhWFX30KHQN9DvAQ4dUDCAU&uact=5
我想将其转换为如下所示的干净网址:
And I want to turn that into nice clean Search URL as below:
https://www.google.com/search?q=laravel+crud+generator
我该如何在问题中提到的任何文本编辑器中使用正则表达式查找/替换?
How can I acheive that using Find/Replace with Regex of any of mentioned text editors in Question ?
推荐答案
我发布的消息是其他人使用该解决方案.
I'm posting that others use the solution.
在记事本++中,请按 CTRL + H
,然后在下面选择正则表达式.
in notepad++ please press CTRL+H
then select Regular expression on below.
然后在上查找以下内容:
此模式:.+&(q = [^&] +).+
并在替换为插入"中: https://www.google.com/search?$1
Then place on Find what:
this pattern: .+&(q=[^&]+).+
and in Replace with insert: https://www.google.com/search?$1
现在,轻松按 Replace
按钮进行一次替换或进行所有替换,按 ALT + A
或 Replace All
按钮.
Now, easily press the Replace
button for single replace or for all replacements press ALT+A
or Replace All
button.
检查 Regex101
但是描述:
1- .+&
在 q
之后找到&
之前的所有字符.因此,这部分包括 https://www.google.com/search?source=hp&ei=Ne4pXpSIHIW_9QOD-rmADw&
1- .+&
find all characters before &
following a q
. So this part includes https://www.google.com/search?source=hp&ei=Ne4pXpSIHIW_9QOD-rmADw&
2- (q = [^&] +)
,我们的目标!我们希望在 q =
之后的所有内容都在下一个&
上.因此,我们搜索以q =开头的字符串,然后搜索不是&
的任何字符. [^&]
表示不是&
的字符,并且 +
表示任何不是& <的字符/code>大于零的时间.这部分将包括
q = laravel + crud + generator
.请注意括号.
2- (q=[^&]+)
, our target! we want everything after q=
up next &
. So we search for a string which started with q= then any character which is not &
. [^&]
means a character that is not &
and +
is saying that any character that is not &
more than zero time. this part will include q=laravel+crud+generator
. Please notice the parentheses.
3- .+
表示任何字符,包括& oq = laravel + crud + generator& gs_l = psy-ab.3..0l8.1294.6845..7289 ...1.0..0.307.3888.0j20j2j1 ...... 0 .... 1..gws-wiz ..... 6..0i131j0i362i308i154i357.PwlZ_932pXo& ved = 0ahahEWEjU9pz4tJrnAhWFX30KHQN9DvAQ4dUDCAU& uact = 5
3- .+
means any character and includes &oq=laravel+crud+generator&gs_l=psy-ab.3..0l8.1294.6845..7289...1.0..0.307.3888.0j20j2j1......0....1..gws-wiz.....6..0i131j0i362i308i154i357.PwlZ_932pXo&ved=0ahUKEwjU9pz4tJrnAhWFX30KHQN9DvAQ4dUDCAU&uact=5
好吧,还记得第2节中的()
吗?那是一个小组.您可以通过此模式 $ groupNumber
在替换中使用组,其中groupNumber是括号的索引.这里我们只有一个()
或实际上只有一组,所以我们的替换语句将为 $ 1
.
ok, remember ()
in section 2? that was a group. you can use groups in replacements by this pattern $groupNumber
which groupNumber is the index of parentheses. Here we have just one ()
or actually just one group, so our replacement statement will be $1
.
最后替换: https://www.google.com/search?$1
,所以第一组中的所有内容都将替换为$ 1.
And finally replacement: https://www.google.com/search?$1
so everything is inside group one will replace with $1.
这篇关于文本编辑器(Sublime Text,Geany,Notepad ++等),用于从URL字符串中删除除一个参数值以外的所有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!