1

I am using below java program to find list of js files as a Substring.

    String str = "jsLib//connect.facebook.net/en_US/fbevents.js , jsLib//connect.facebook.net/en_US/fbevents2.js;";
    String patternStr = "(\\/.*?\\.js)";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(html);
    if (matcher.find()) {
        System.out.println("Count:" + matcher.groupCount());
        jsLib = matcher.group(1);
        jsLib = jsLib.substring(jsLib.lastIndexOf('/') + 1, jsLib.length());
        System.out.println("jsLib:" + jsLib);
    }

Regex : I used String patternStr="(\\/.*?\\.js)";

Expected Result : both fbevents.js and fbevents2.js should be matched and part of result

Actual Result : only fbevents.js is matched

0

1 Answer 1

3

You may get all your results using while loop and a regex like [^/]*\.js:

String str = "jsLib//connect.facebook.net/en_US/fbevents.js , jsLib//connect.facebook.net/en_US/fbevents2.js;";
String patternStr = "[^/]*\\.js";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
    System.out.println("jsLib:" + matcher.group());
}

Output:

jsLib:fbevents.js
jsLib:fbevents2.js

See the Java demo and the regex demo.

The [^/]*\.js pattern matches any 0+ chars other than / (with [^/]*) and then a .js substring.

Sign up to request clarification or add additional context in comments.

Comments

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.