1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch, but when I try to test it through cis.upenn.edu/~matuszek/General/RegexTester/regex-tester.html it still shows "Pattern does not match" :-( Not sure if the regex tester is not working fine...I will try this in the actual code and see :) thanks again.

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.