Skip to main content

Java Regular Expressions

Java Regular Expressions, also known as Java Regex or Java Pattern, is a powerful tool that allows you to search for, manipulate, and validate strings based on a pattern. Regular expressions are a sequence of characters that define a search pattern. Java has built-in support for regular expressions through the java.util.regex package.

Here are some basic concepts to understand when working with Java regex:

  1. Character classes: These are sets of characters that match a single character in the text. For example, the character class [abc] matches any one of the characters a, b, or c.

  2. Quantifiers: These specify how many times a pattern should match. For example, the quantifier * means "zero or more times", and the quantifier + means "one or more times".

  3. Anchors: These specify where a pattern should match in the text. For example, the anchor ^ matches the beginning of the text, and the anchor $ matches the end of the text.

  4. Grouping: This allows you to group patterns together and apply quantifiers to them. For example, the pattern (ab)+ matches one or more occurrences of the characters "ab".

Here are some common regular expression patterns and their meanings:

  1. . : Matches any character except newline.

  2. * : Matches zero or more occurrences of the preceding character. For example, a* matches zero or more occurrences of the letter 'a'.

  3. + : Matches one or more occurrences of the preceding character. For example, a+ matches one or more occurrences of the letter 'a'.

  4. ? : Matches zero or one occurrence of the preceding character. For example, a? matches zero or one occurrence of the letter 'a'.

  5. [] : Matches any character in the brackets. For example, [abc] matches any one of the characters 'a', 'b', or 'c'.

  6. [a-z] : Matches any character from 'a' to 'z'.

  7. [^] : Matches any character not in the brackets. For example, [^abc] matches any character except 'a', 'b', or 'c'.

  8. \d : Matches any digit (0-9).

  9. \w : Matches any word character (a-z, A-Z, 0-9, _).

  10. \s : Matches any whitespace character (space, tab, newline).

  11. ^ : Matches the start of the line.

  12. $ : Matches the end of the line.

  13. | : Matches either the expression before or after the pipe. For example, a|b matches either the letter 'a' or 'b'.

  14. () : Groups expressions together. For example, (abc)+ matches one or more occurrences of the sequence 'abc'.

  15. \\ : Escapes a special character. For example, to match a literal dot character, you need to escape it like this: \\..

import java.util.regex.*;

public class RegexExample {
    public static void main(String[] args) {
        String input = "The quick brown fox jumps over the lazy dog";
        Pattern pattern = Pattern.compile("\\b\\w{5}\\b");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

Regular expression generally used to manipulate text string such as finding and replacing text of given string etc. That is very useful mechanism which can search and replace particular text pattern in long text string.

//For Pattern and Matcher
import java.util.regex.*; 

public class Test {

  public static void main(String args[]) 
  { 
    //Create object to search Pattern
    Pattern pattern = Pattern.compile("Code"); 

    Matcher match = pattern.matcher("Java Code VS Python Code"); 
    
    //Display pattern location
    while(match.find()){
      System.out.println("Pattern Exist from " + 
        match.start() + " to " + (match.end()-1)); //View position
    } 
  } 
}
Java Regular Expressions Example
Pattern Exist from 5 to 8
Pattern Exist from 20 to 23

java.util.regex.Pattern Public Method

Method Syntax
pattern public java.lang.String java.util.regex.Pattern.pattern()
asPredicate public java.util.function.Predicate java.util.regex.Pattern.asPredicate()
splitAsStream public java.util.stream.Stream java.util.regex.Pattern.splitAsStream(java.lang.CharSequence)
toString public java.lang.String java.util.regex.Pattern.toString()
flags public int java.util.regex.Pattern.flags()
matches public static boolean java.util.regex.Pattern.matches(java.lang.String,java.lang.CharSequence)
split public java.lang.String[] java.util.regex.Pattern.split(java.lang.CharSequence,int)
split public java.lang.String[] java.util.regex.Pattern.split(java.lang.CharSequence)
compile public static java.util.regex.Pattern java.util.regex.Pattern.compile(java.lang.String)
compile public static java.util.regex.Pattern java.util.regex.Pattern.compile(java.lang.String,int)
matcher public java.util.regex.Matcher java.util.regex.Pattern.matcher(java.lang.CharSequence)
quote public static java.lang.String java.util.regex.Pattern.quote(java.lang.String)
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
equals public boolean java.lang.Object.equals(java.lang.Object)
hashCode public native int java.lang.Object.hashCode()
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()

java.util.regex.Matcher public method

Method Syntax
pattern public java.util.regex.Pattern java.util.regex.Matcher.pattern()
hitEnd public boolean java.util.regex.Matcher.hitEnd()
requireEnd public boolean java.util.regex.Matcher.requireEnd()
toMatchResult public java.util.regex.MatchResult java.util.regex.Matcher.toMatchResult()
usePattern public java.util.regex.Matcher java.util.regex.Matcher.usePattern(java.util.regex.Pattern)
groupCount public int java.util.regex.Matcher.groupCount()
lookingAt public boolean java.util.regex.Matcher.lookingAt()
regionStart public int java.util.regex.Matcher.regionStart()
regionEnd public int java.util.regex.Matcher.regionEnd()
hasTransparentBounds public boolean java.util.regex.Matcher.hasTransparentBounds()
useTransparentBounds public java.util.regex.Matcher java.util.regex.Matcher.useTransparentBounds(boolean)
hasAnchoringBounds public boolean java.util.regex.Matcher.hasAnchoringBounds()
useAnchoringBounds public java.util.regex.Matcher java.util.regex.Matcher.useAnchoringBounds(boolean)
end public int java.util.regex.Matcher.end()
end public int java.util.regex.Matcher.end(int)
end public int java.util.regex.Matcher.end(java.lang.String)
region public java.util.regex.Matcher java.util.regex.Matcher.region(int,int)
group public java.lang.String java.util.regex.Matcher.group()
group public java.lang.String java.util.regex.Matcher.group(int)
group public java.lang.String java.util.regex.Matcher.group(java.lang.String)
toString public java.lang.String java.util.regex.Matcher.toString()
matches public boolean java.util.regex.Matcher.matches()
replaceFirst public java.lang.String java.util.regex.Matcher.replaceFirst(java.lang.String)
replaceAll public java.lang.String java.util.regex.Matcher.replaceAll(java.lang.String)
quoteReplacement public static java.lang.String java.util.regex.Matcher.quoteReplacement(java.lang.String)
find public boolean java.util.regex.Matcher.find(int)
find public boolean java.util.regex.Matcher.find()
start public int java.util.regex.Matcher.start(java.lang.String)
start public int java.util.regex.Matcher.start()
start public int java.util.regex.Matcher.start(int)
reset public java.util.regex.Matcher java.util.regex.Matcher.reset()
reset public java.util.regex.Matcher java.util.regex.Matcher.reset(java.lang.CharSequence)
appendReplacement public java.util.regex.Matcher java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer,java.lang.String)
appendTail public java.lang.StringBuffer java.util.regex.Matcher.appendTail(java.lang.StringBuffer)
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
equals public boolean java.lang.Object.equals(java.lang.Object)
hashCode public native int java.lang.Object.hashCode()
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()

java.util.regex.PatternSyntaxException public method

Method Syntax
getDescription public java.lang.String java.util.regex.PatternSyntaxException.getDescription()
getPattern public java.lang.String java.util.regex.PatternSyntaxException.getPattern()
getIndex public int java.util.regex.PatternSyntaxException.getIndex()
getMessage public java.lang.String java.util.regex.PatternSyntaxException.getMessage()
printStackTrace public void java.lang.Throwable.printStackTrace()
printStackTrace public void java.lang.Throwable.printStackTrace(java.io.PrintWriter)
printStackTrace public void java.lang.Throwable.printStackTrace(java.io.PrintStream)
fillInStackTrace public synchronized java.lang.Throwable java.lang.Throwable.fillInStackTrace()
getCause public synchronized java.lang.Throwable java.lang.Throwable.getCause()
initCause public synchronized java.lang.Throwable java.lang.Throwable.initCause(java.lang.Throwable)
toString public java.lang.String java.lang.Throwable.toString()
getLocalizedMessage public java.lang.String java.lang.Throwable.getLocalizedMessage()
getStackTrace public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace()
setStackTrace public void java.lang.Throwable.setStackTrace(java.lang.StackTraceElement[])
addSuppressed public final synchronized void java.lang.Throwable.addSuppressed(java.lang.Throwable)
getSuppressed public final synchronized java.lang.Throwable[] java.lang.Throwable.getSuppressed()
wait public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
wait public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
wait public final void java.lang.Object.wait() throws java.lang.InterruptedException
equals public boolean java.lang.Object.equals(java.lang.Object)
hashCode public native int java.lang.Object.hashCode()
getClass public final native java.lang.Class java.lang.Object.getClass()
notify public final native void java.lang.Object.notify()
notifyAll public final native void java.lang.Object.notifyAll()




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment