正则表达式用于解析URL的子字符串

时间:2016-09-02 10:26:26

标签: c# regex

我有以下字符串:

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,但我想从结果中排除双引号。

任何想法如何使用正则表达式删除双引号?

感谢。

1 个答案:

答案 0 :(得分:1)

这取决于您如何阅读匹配的结果。 您可以使用除双引号之外的所有内容的其他组:

\"((\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4}))\"

或者您可以使用正面的lookbehind,以确保之前的字符是双引号:

(?<=\")(\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4})