27 August 2023

Regular Expression Java - learngreen.net

 

Regular expression Java - learngreen.net

  A string of characters known as a regular expression designates a search pattern. It is an effective tool for pattern-based string manipulation, matching, and searching. For tasks like string validation, text processing, and data extraction, regular expressions are utilized in many different programming languages and technologies.

The java.util.regex package in Java contains two primary classes: Pattern and Matcher, which are used to implement regular expressions.

The Java programming language's Pattern class is used to define and communicate with regular expression patterns. It is a part of the package java.util.regex. It provides methods for compiling, storing, and manipulating regular expressions for a range of string matching and manipulation applications. The Pattern class is examined in greater detail here

Making a Pattern Object:- To start using regular expressions, create a Pattern object using the Pattern.compile( ) function. This method compiles the regular expression into an internal representation that may be used to match input strings efficiently.



  package Java;
  import java.util.regex.Pattern;
  public class Vv {
  public static void main(String[] args) {
	 
  /* Using Pattern.matches() to check if input string matches a pattern */
  /* Pattern:- "Navne.*t"
   * Description: Matches strings that start with "Navne",
   *  have any characters (including none) in between,
   * and end with "t".
   */
  System.out.println(Pattern.matches("Navne.*t", "Navneet")); 
 
  /* Pattern: "Navne\\*t"
   * Description: Matches the string "Navne*t" 
   * literally, where the '*' character is treated 
   * as a literal '*'.
   */
 
  System.out.println(Pattern.matches("Navne\\*t", "Navneet"));
  // Output-> true
  //          false
	 }
   }

 


 package Java;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  public class PCE {
  public static void main(String[] args) {
  // Regular expression pattern to find occurrences of "Java"
  String regex = "\\bJava\\b";
  // Input text
  String text = "Java is a popular programming language. Java is used worldwide.";
  // Compile the regular expression pattern
  Pattern pattern = Pattern.compile(regex);
  // Create a Matcher object using the compiled pattern
  Matcher matcher = pattern.matcher(text);
  // Find and print all occurrences of "Java"
  System.out.println("Occurrences of 'Java':");
  while (matcher.find()) {
  System.out.println("Found at index " + matcher.start());
	 }
        }
   }

 compile(String regex)

It is used to create a pattern from the given regular expression.



  Pattern pattern = Pattern.compile("\\d+");
  

 compile(String regex, int flags)

It is used to create a pattern using the supplied flags and the specified regular expression.

flags( )
It serves to return the match flags for this pattern.



  int flags = pattern.flags( );
  

 matcher(CharSequence input)

It is utilized to build a matcher that will compare the input received with this pattern.



  boolean matches = pattern.matcher("12345").matches( );
  

 matches(String regex, CharSequence input)

It attempts to match the input against the specified regular expression after compiling it.



  boolean matches = pattern.matcher("12345").matches( );
  

 pattern( )

It is used to return the regular expression that was used to create this pattern.



  String regex = pattern.pattern();
  

 quote(String s)

For the given String, it is used to return a literal pattern String.


split(CharSequence input)

It is used to divide the input sequence that is given around instances of this pattern.



  String[] parts = pattern.split("one 2 three 45");
  

 split(CharSequence input, int limit)

It is used to divide the input sequence that is given around instances of this pattern. The limit parameter regulates how frequently the pattern is used.



  String[] parts = pattern.split("one 2 three 45", 2); // Results in ["one ", " three 45"]
  

 toString( )

The string representation of this pattern is returned using it.

Java's Matcher class, which is a component of the java.util.regex package, is used to compare an input string to a regular expression pattern. It offers ways to carry out different matching operations, look for occurrences, and extract matched substrings. Here is a description of the Matcher class's principal methods
By calling the matcher(CharSequence input) method on a Pattern object, you can build a Matcher object. The string you want to compare to the pattern is represented by the CharSequence input.



  Pattern pattern = Pattern.compile("pattern");
  Matcher matcher = pattern.matcher(inputString);
  

 Matching Operations:-

The Matcher class provides several methods to perform matching operations:-

matches( ):- This method checks if the entire input sequence matches the pattern.



  boolean isMatch = matcher.matches();
  

 lookingAt( ):- This method attempts to match the pattern starting at the beginning of the input sequence.


  boolean isMatch = matcher.lookingAt();
  

 find( ):- This method attempts to find the next subsequence in the input sequence that matches the pattern.


  boolean isFound = matcher.find( );
  

 Extracting Matched Substrings:-

The following methods are used to extract matched substrings:-

group( ):- Returns the matched substring from the last successful match.



  String matchedText = matcher.group( );
  

 group(int group):-  Returns the matched substring for the specified capturing group index.



  String groupText = matcher.group(1); // Get matched text for capturing group 1
 

 start( ) and end( ):- These methods return the start and end indices of the last match, respectively


  int startIndex = matcher.start( );
  int endIndex = matcher.end( );
 

 start(int group) and end(int group):- Similar to the previous methods, but they provide indices for a specific capturing group.


 int groupStart = matcher.start(1); // Start index of capturing group 1
 int groupEnd = matcher.end(1); // End index of capturing group 1
 

   In regular expressions, a character class is a way to offer a collection of characters that can all match the same character at a specific point in a string. Using character classes, you can specify a range or a collection of characters that you want to match. They are encapsulated by square brackets [...]. Following is a description of character classes and how they work

Beginner Character Classes

Various Characters The match could be defined as just one character. The character 'a', for instance, will match with [a].

Character Ranges:-  You can specify a range of characters by using a hyphen. For instance, any lowercase letter matches [a-z].

A character class that begins with a caret (') matches any character besides those listed because it is negated. For instance, [0-9] matches every character that is not a digit.

Predefined Character Classes: The regular expression engine in Java supports the following predefined character classes.

\d :matches any digit character(equivalent to [0-9]).

\D: Matches any non-digit character(equal to [0-9]).

(Represented by [a-zA-Z0-9_]) 

\w:  Compatible with all word characters.

\W: Any character that isn't a word can be represented by the character set [a-zA-Z0-9_].

Any whitespace character, such as a space, tab, or newline, is matched by the keyword  \s.

Any character that isn't a blank space that starts with \S matches.

Combining Characters: Within a character class, you can combine characters and character ranges. For instance, any uppercase or lowercase letter matches [A-Za-z].

Escape specific Characters:-  Some characters, such as [ ,   ] ,^ , -,  etc., have specific meanings inside character classes. You must use a backslash ( \ ) to escape these characters if you wish to match them exactly.



No comments:

Post a Comment