-4

Possible Duplicate:
Regex to match URL

Is there a regular expression to return a http value from a string?

So

sdfads saf as fa http://www.google.com some more text

becomes

http://www.google.com

0

2 Answers 2

2

a very simple approach:

https?://\S+

if you must check for valid urls the regex is much more complex

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

Comments

0

Here is simple and working example that contains retrieving searched pattern and using it to replace whole input:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regextest {

    static String[] matchThese = new String[] {
            "sdfads saf as fa http://www.google.com some more text",
            "sdfads fa http://www.dupa.com some more text",
            "should not match http://" };

    public static void main(String[] args) {

        String regex = "(https?://|www)\\S+";
        Pattern p = Pattern.compile(regex);

        System.out.println("Those that match are replaced:");
        for (String input : matchThese) {
            if (p.matcher(input).find()) {

                Matcher matcher = p.matcher(input);
                matcher.find();

                // Retrieve matching string
                String match = matcher.group();

                String output = input.replace(input, match);
                System.out.println(output);

            }
        }

    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.