Java: Regex To Check If Content Contains All Letters Or Not


Here is a method using the Java programming language that checks via regex if the String content contains all letters or not. The letters in the String content, either uppercase and/or lowercase will be accepted. The value returned is a boolean.

1
2
3
4
public static boolean isLetters(String content) {
  Pattern p = Pattern.compile("^[a-zA-Z]+$");
  return p.matcher(content).matches();	
}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Regex To Get All Contents Within Curly Brackets


Let us say your string contains this data

1
{"1":"one", "2":"two"},{"3":"three", "4":"four"},{"5":"five", "6":"six"}

While regex can slow down performance, I decided to use it for this situation because I can do it using only a regex pattern. This regex will get all instances of a substring between two curly brackets { and }. In the example above, the result should be 3 entries since there are 3 sets of curly brackets. Please see the regular expression below.

1
\\{[^}]*\\}

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

A Good Regex For Creating Usernames


A regular expression (called regex) is a way for a programmer to instruct how a program should look for a specified pattern in text and then what it should do when each pattern match is found. Rather than going through each character of a string and doing matches, regex makes life easier for programmers to do search and matches. This sample regex is a good pattern for use in creating usernames.

The regex below means that only alphanumeric (only lowercase letters) are allowed including an underscore and a dot.

1
^[a-z0-9_.]$

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails