Regular Expression in Java

Regular Expression in Java

In this post, we will discuss regular expression. Regular Expression is found in java.util.regex.

They are used for the pattern match in Java strings and define the search pattern for strings.

Regular Expression Syntax:

  • ^ match the start of a line.
  • $ match the end of the line.
  • . Match every single letter instead of a new line.
  • [ ] match every letter in the brackets.
  • [^ ] match all the letter’s not in brackets.
  • \d any digit [0-9]
  • \D any non-digit [^0-9]
  • \w any alpha character [a-zA-Z]
  • \W any non-alphabet character [^a-zA-Z]
  • \s any whitespace char
  • \S nonwhite space char

Regular Expression Quantifiers:

  • ? Appearing once or not at all
  • + At least one occurrence
  • 0 or more occurrence
  • { n} exactly n occurrences
  • {n,} n or more occurrences
  • {n-m} at least n occurrence up to m

Split:

Let’s suppose we have a string and we want to remove the blank/white spaces from that string and want to show it in string array we use a simple string.split() function and pass empty string so that it will remove empty spaces from a string.

Output

regular expression split output
regular expression split output

 

If we have multiple spaces in our string then this won’t be work, and we have to write \s+ as \s is used for white space and in this case, we have multiple so we use \s+ and as we have to pass a string as argument and \ is a reserved word, so we use \.

Now for blank space we use \s.

For dot (.)  We use .

Replace ALL:

Now if I want to replace all the non-integer characters like alphabetic or special characters, then we use to replace all function.

Output

regular expression matcher in java output
Regular expression matcher in Java output

ReplaceAll(String regex, String replacement);

regular expression replace all in java
regular expression replaces all in Java

We also do this in another way like

In above code ^ represent not and the scene we have multiple numbers so we add + at the last and it means it will ignore numbers 1 to 9 and replace all other numbers will null value.

And same if we do not want to replace all the alphabets then the code is:

Matches:

This function is used to match a string with some pattern. If it matches then it will return true otherwise false.

Output

regular expression matcher in java output
Regular expression matcher in Java output

Now what if I want to check the string only contain alphabets the regex string is

“[a-zA-Z]+”.

 

Email Validation using regex in java:

Code for email validation using regular expression in Java:

Output

Regular expression email validation java output
Regular expression email validation Java output

Leave a Reply

Your email address will not be published. Required fields are marked *