您可以使用环视语法来匹配?cid=something
URL 前面和后面的'
这种模式应该有效:
(?<=\Qhttps://www.mydomain.com/shop/bags\E)\?cid=[^']++(?=')
如果你用你的替换替换那个模式,那么从?cid
until的整个位'
将被替换。
这是 Java 中的示例(忽略略有不同的语法):
public static void main(String[] args) {
final String[] in = {
"onclick=\"location.href='https://www.mydomain.com/shop/bags?cid=Black'",
"onclick=\"location.href='https://www.mydomain.com/shop/bags?cid=Beige'",
"onclick=\"location.href='https://www.mydomain.com/shop/bags?cid=Green'"
};
final Pattern pattern = Pattern.compile("(?<=\\Qhttps://www.mydomain.com/shop/bags\\E)\\?cid=[^']++(?=')");
for(final String string : in) {
final Matcher m = pattern.matcher(string);
final String replaced = m.replaceAll("SOMETHING_ELSE");
System.out.println(replaced);
}
}
输出
onclick="location.href='https://www.mydomain.com/shop/bagsSOMETHING_ELSE'
onclick="location.href='https://www.mydomain.com/shop/bagsSOMETHING_ELSE'
onclick="location.href='https://www.mydomain.com/shop/bagsSOMETHING_ELSE'
显然,这假设您的工具支持环视。
如果您只是直接使用 Perl 而不是通过您的魔术工具,这肯定会起作用
perl -pi -e '/s/(?<=\Qhttps://www.mydomain.com/shop/bags\E)\?cid=[^\']++(?=\')/SOMETHING_ELSE/g' *some_?glob*.pattern
编辑
另一个想法是使用捕获组和反向引用,替换
(\Qhttps://www.mydomain.com/shop/bags\E)\?cid=[^']++
和
$1SOMETHING_ELSE
Java中的另一个测试用例:
public static void main(String[] args) {
final String[] in = {
"onclick=\"location.href='https://www.mydomain.com/shop/bags?cid=Black'",
"onclick=\"location.href='https://www.mydomain.com/shop/bags?cid=Beige'",
"onclick=\"location.href='https://www.mydomain.com/shop/bags?cid=Green'"
};
final Pattern pattern = Pattern.compile("(\\Qhttps://www.mydomain.com/shop/bags\\E)\\?cid=[^']++");
for(final String string : in) {
final Matcher m = pattern.matcher(string);
final String replaced = m.replaceAll("$1SOMETHING_ELSE");
System.out.println(replaced);
}
}
输出:
onclick="location.href='https://www.mydomain.com/shop/bagsSOMETHING_ELSE'
onclick="location.href='https://www.mydomain.com/shop/bagsSOMETHING_ELSE'
onclick="location.href='https://www.mydomain.com/shop/bagsSOMETHING_ELSE'