Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

规则求教: 正则匹配之后的自动路径拼接 #1071

Open
Fredida opened this issue Apr 30, 2024 · 3 comments
Open

规则求教: 正则匹配之后的自动路径拼接 #1071

Fredida opened this issue Apr 30, 2024 · 3 comments

Comments

@Fredida
Copy link

Fredida commented Apr 30, 2024

有n个待匹配的url例如:
https://domain/path-aaa/ran02/path-bbb/file-d
https://domain/path-aaa/ran67/path-ccc/file-e
其中ran后面为定长2位的随机数

本地目录X:\mock-domain\path-aaa\randxx分别有子目录path-bbb和path-ccc存放了file-d和file-e
写了一条正则rule匹配这类url让他们分别访问对应子目录里的文件:
/https://domain/path-aaa/ran\d\d/path-bbb/file-d/ file://X:\mock-domain\path-aaa\randxx

但实际测试对于任何匹配上的url都会只自动访问到X:\mock-domain\path-aaa\randxx,请问有简洁的正则办法让程序在randxx之后根据url自动拼接访问吗?

另外不知道whistlek可不可以debug rule(规则)的解析与执行的过程,这样可以自行调试发现自己写的rule在哪里系统解析结果和预想的不一样,就能少提issue来麻烦作者了。

@avwo
Copy link
Owner

avwo commented May 6, 2024

有两种写法:

# 正则
/https://domain/path-aaa/ran(\d\d)/path-bbb/file-/ file://X:\mock-domain\path-aaa\rand$1

# 通配符
^https://domain/path-aaa/ran*/path-bbb/file-* file://X:\mock-domain\path-aaa\rand$1

参考:https://wproxy.org/whistle/pattern.html

@Fredida
Copy link
Author

Fredida commented May 14, 2024

感谢作者的帮助,抱歉之前表述的有歧义,其实是想把
/https://domain/path-aaa/ran\d\d/path-bbb/file-1
/https://domain/path-aaa/ran\d\d/path-ccc/file-2
都匹配到
file://X:\固定dir\
(X:\固定dir下有path-bbb\file-1和path-ccc\file-2等)
请问该如何撰写规则呢?

提问之前有阅读过帮助文档pattern.html,请问这段描述

最多支持10个子匹配 $0...9,其中$0表示整个请求url,其它跟正则的子匹配一样 /[^?#]/([^\/]+).html/ protocol://...$1...

应该理解为$1为正则第一次匹配之后剩余的后续url部分?那么$2(对应的子匹配)是什么呢?

@avwo
Copy link
Owner

avwo commented May 15, 2024

跟正则的子匹配一样,试试:

^https://domain/path-aaa/ran*/** file://X:\固定dir\$2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants