2

I have the following string:

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
                }
            });
        }

I am trying to parse the above string to list all the URL's which starts from /example and ends with file name like abc.txt.

So the desired list should be: /example/abc/include.txt

I tried with the following regex :

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

But it list two URLs as below:

/example/abc/include.txt
/example/abc/dontinclude.txt

I changed the above regex to :

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

This returns the required URL but I want exclude the double quotes from the result.

Any Idea how to remove the double quotes using the regex?

Thanks.

1 Answer 1

1

It depends on how you read the matched results. You could use another group surrounding everything but the double quotes:

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

or you could use a positive lookbehind, that ensures that the character before is a double quote:

(?<=\")(\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4})
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply. The positive look-forward (?<=\") is not c# regex flavor. I tried the same in c# flavor (?=\")(\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4}). But it didn't work.
The positive lookbehind is C# regex flavor. (see: msdn.microsoft.com/de-de/library/az24scfc(v=vs.110).aspx). Have you escaped the double quote correct? @"(?<=\"")(\/)[^\s\/]?(example\/)(\w+\/)*(\w+\.\w{3,4})"
Yes @Yosh you are right. I tested it in wrong tool. Its working fine now. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.