Abhishek
Abhishek

Reputation: 6900

Java regex for validating url, boolean, string

I am in the process of formulating a regex for validating a text area that contains entries of the following format,

an url, boolean(true or false), string(with or without spaces)

An example is as follows,

http://www.yahoo.com, true, web mail site
http://www.google.com, false, a search site

So I was trying to formulate a regex for each line as below,

(^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$)(,(true|false))(,(.*))

Hence I can check each line, but this regex is not working. The whole regex fails to match the type of comma separated string. Also is there some way I can make this regex check for multiple lines and validating this pattern?

Upvotes: 1

Views: 16797

Answers (1)

NullUserException
NullUserException

Reputation: 85478

If the line breaks are your only problem, you could use the Pattern.MULTILINE flag:

Pattern.compile("^((?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]), (true|false), (.*)$", Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);

You can also embed the flag(s):

Pattern.compile("(?mi)^((?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]), (true|false), (.*)$",);

I took the liberty of using a different regex for your URL (it's from Regex Buddy). This also will put everything in a capture group.


Demo: http://ideone.com/I9vpB

public static void extract(String str) {

    Pattern regex = Pattern.compile("(?mi)^((?:https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|$!:,.;]*[A-Z0-9+&@#/%=~_|$]), (true|false), (.*)$");

    Matcher m = regex.matcher(str);
    while (m.find()) {
        System.out.println("URL:  " + m.group(1));
        System.out.println("Bool: " + m.group(2));
        System.out.println("Text: " + m.group(3) + "\n");
    }
}

public static void main (String[] args) throws java.lang.Exception
{
    String str = "http://www.yahoo.com, true, web mail site\nhttp://www.google.com, false, a search site";
    extract(str);
}

Outputs:

URL:  http://www.yahoo.com
Bool: true
Text: web mail site

URL:  http://www.google.com
Bool: false
Text: a search site

Upvotes: 1

Related Questions