我有以下字符串:
function init() {
$.get("/example/abc/include.txt", function(script) {
code goes here
});
$.get("<http>://abc.com/example/abc/dontinclude.txt", function (script) {
code goes here
}
});
}
我正在尝试解析上面的字符串以列出所有从/example 开始并以文件名如 abc.txt 结尾的 URL。
因此所需的列表应该是:
/example/abc/include.txt
我尝试使用以下正则表达式:
(\/)[^\s\/]?(example\/)(\w+\/)*(\w+.\w{3,4})
但是它列出了如下两个 URL:
/example/abc/include.txt
/example/abc/dontinclude.txt
我将上面的正则表达式更改为:
\"(\/)[^\s\/]?(example\/)(\w+\/)*(\w+.\w{3,4})\"
这会返回所需的 URL,但我想从结果中排除双引号。
知道如何使用正则表达式删除双引号吗?
谢谢。
最佳答案
这取决于你如何阅读匹配的结果。 除了双引号外,您可以使用另一组来包围所有内容:
\"((\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4}))\"
或者您可以使用正向后视,确保前面的字符是双引号:
(?<=\")(\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4})
关于c# - 用于解析 URL 子字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39290145/