Regular expression to accept only alphabets
This regular expression refers to a pattern which accepts alphabets only.
Example
package com.w3schools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
private static final String PATTERN = "[a-zA-Z]+\.?";
public static void main(String args[]){
List<String> values = new ArrayList<String>();
values.add("Jai");
values.add("Jai5");
values.add("12345");
Pattern pattern = Pattern.compile(PATTERN);
for (String value : values){
Matcher matcher = pattern.matcher(value);
System.out.println(matcher.matches());
}
}
} |
package com.w3schools;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
private static final String PATTERN = "[a-zA-Z]+\.?";
public static void main(String args[]){
List<String> values = new ArrayList<String>();
values.add("Jai");
values.add("Jai5");
values.add("12345");
Pattern pattern = Pattern.compile(PATTERN);
for (String value : values){
Matcher matcher = pattern.matcher(value);
System.out.println(matcher.matches());
}
}
}
Output