Java Regular Expressions

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()


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







© 2021, kalkicode.com, All rights reserved