Reputation: 53
How can I make this regex match white spaces? Currently, it can only match the following:
abcdatcsdotuniversitydotedu
I would like it to mach the following:
abcd at cs dot university dot edu
This is the Regex:
([A-Za-z][A-Za-z0-9.\\-_]*)\\s[ ]?(at)[ ]*([A-Za-z][A-Za-z0-9\\-_(dot)]*[ ]?(dot)[ ]*[A-Za-z]+)
Upvotes: 0
Views: 7538
Reputation: 839
public static boolean isAlphaNumericWithWhiteSpace(String text) {
return text != null && text.matches("^[\\p{L}\\p{N}ın\\s]*$");
}
\p{L} matches a single code point in the category "letter".
\p{N} matches any kind of numeric character in any script.
I am using this code.
Upvotes: 1
Reputation: 167982
\s
matches a white-space character and when this is used in a java string you need to escape the \
so it would be \\s
. If you want to match zero-or-more white-space then use \\s*
.
This will match a single domain and TLD:
([A-Za-z][A-Za-z0-9.\\-_]*)\\s*(at)\\s*([A-Za-z][A-Za-z0-9\\-_()]*\\s*(dot)\\s*[A-Za-z]+)
However, you are trying to match multiple levels of sub-domains so you need to wrap the domain part of the regular expression ([A-Za-z][A-Za-z0-9\\-_()]*\\s*(dot)\\s*
in ()+
to get one-or-more of them:
([A-Za-z][A-Za-z0-9.\\-_]*)\\s*(at)\\s*(([A-Za-z][A-Za-z0-9\\-_()]*\\s*(dot)\\s*)+[A-Za-z]+)
^ ^^
Something like this:
public class RegexpMatch {
static Pattern Regex = Pattern.compile(
"([A-Za-z][A-Za-z0-9.\\-_]*)\\s*(at)\\s*(([A-Za-z][A-Za-z0-9\\-_()]*\\s*(dot)\\s*)+[A-Za-z]+)"
);
public static void main( final String[] args ){
final String[] tests = {
"abcdatcsdotuniversitydotedu",
"abcd at cs dot university dot edu"
};
for ( final String test : tests )
System.out.println( test + " - " + ( Regex.matcher( test ).matches() ? "Match" : "No Match" ) );
}
}
Which outputs:
abcdatcsdotuniversitydotedu - Match
abcd at cs dot university dot edu - Match
Upvotes: 3