1

I have a requirement which states as below

The following special characters are allowed with restrictions in the input string:

. ""(),:;<>@[\]

The restrictions for the special characters are that they must only be used when contained between quotation marks.

For a simple test for ":" within input string, I wrote code as below:

private static void testEmailPattern() {
    String email = "Test\":\"mail";
    String PATTERN = "[\":\"]*";
    boolean isValidEmail = email.matches(PATTERN);
    System.out.println("Status: " + isValidEmail);
}

but this code returns false as opposed to true.

Edit: After reading comments, I modified that code to this, but it is still showing false.

I modified my code and made it as below:

public class TestFeatures {
    private Pattern pattern;
    private Matcher matcher;

    private static final String PATTERN = "[.*\":\".*]*";

    public TestFeatures() {
        initEmailPattern();
    }

    private void initEmailPattern() {
        pattern = Pattern.compile(PATTERN);     
    }

    public boolean validate(final String hex) {
        matcher = pattern.matcher(hex);
        return matcher.matches();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        testEmailPattern();
    }

    private static void testEmailPattern() {
        String email = "Test\":\"[email protected]";
        TestFeatures thisClazz = new TestFeatures();
        boolean isValidEmail = thisClazz.validate(email);
        System.out.println("Status: " + isValidEmail);
    }
}
3
  • Replace your brackets with parentheses, and don't use matches() (or add .* at the beginning and the end of your regex). Commented Jan 9, 2014 at 13:09
  • 4
    My homework senses are tingling! Commented Jan 9, 2014 at 13:10
  • 1
    matches: "[t]ells whether or not this string matches the given regular expression". Commented Jan 9, 2014 at 13:16

2 Answers 2

3

You want positive lookahead and positive lookbehind.

(.*(?<=")[.\x20(),:;<>@\[\]"](?=").*)+

Description

Regular expression visualization

Sample code

String[] tests = {
       "Test:mail",
       "Test\":\"mail",
       "Test\"ll",
       ".\".",
       "foo\"\"\""
};

String re = "(.*(?<=\")[.\\x20(),:;<>@\\[\\]\"](?=\").*)+";

int len=tests.length;
for(int i=0; i<len;i++) {
    System.out.format("Test %d: %s >> %s\n" , i+1, tests[i], tests[i].matches(re));
}

Output

Test 1: Test:mail >> false
Test 2: Test":"mail >> true
Test 3: Test"ll >> false
Test 4: .". >> false
Test 5: foo""" >> true
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comments. I modified my code and changed in Original POst
1

[...] is a character class. Character classes match a single character, and most character's meanings are changed when they are contained within a character class. When you have [.*\":\".*], that matches any of the characters ., *, \", :, \", ., or *. (The last three characters are redundant.) * repeats the previous item zero or more times. As Alex said, .matches() will only return true if the regex matches the entire string. Using your regex, [.*\":\".*]*, .matches() will only return true for strings that look somewhat like this: :.\"*.*..\":**\":::.\"*.

I think the regex you want is:

[^. (),:;@\"\\[\\]\\\\*"]*(?:".*")?[^. (),:;@\"\\[\\]\\\\]*

Before any quotation marks, any character is allowed except for the Specified Special Characters (hereafter referred to as the "SScs"). This is fulfilled by the first part of the regex: [^. (),:;@\"\\[\\]\\\\*"]*. It matches an arbitrarily sized sequence of characters that does not include the SSCs. After all the quotation marks in the string, any characters are allowed except for the SScs. That is what [^. (),:;@\"\\[\\]\\\\]* is there for at the end of the regex. It will match any sequence of characters at the end of the string, as long as it does not include the SSCs. In the middle of the regex, there is (?:".*")?. Because any character is allowed inside quotation marks, this will match a quotation mark followed by any sequence of characters, followed by another quotation mark. However, because the string may not contain any quotation marks, it is made optional by the question mark.

You can find a full demonstration and explanation of the parts of the regex here.

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.