Learn Regular Expressions in Java

Regular expressions in Java

Learn Regular expressions in Java

მიმოხილვა:

Java provides regular expression packages that are useful for software developers and testers alike. Java regular expressions are varied, versatile and allow developers to define string patterns that can be used to search, edit or manipulate text. The java.util.regex API comprises multiple classes each of which has a specific responsibility. For example, the java.util.regex pattern is used for defining patterns while the java.util.regex.Matcher is used for matching operations based on the patterns defined by the java.util.regex.Pattern კლასის. It is just about using the appropriate class for the purpose.

In this article, we will check how regular expressions can be used in Java applications.








Do you know – Steps to work with Java Persistence API?

Why we need regular expression?

Regular expressions can be immensely useful especially in large software development projects with long lines of code. In such projects, software developers need to do several things such as creating reusable blocks of codes; refactoring or cleaning codes and putting comments for codes. Now, imagine doing that manually over a large codebase. That would be a colossal waste of time and productivity. This is where regular expressions are so helpful. Software developers or even the testers can define specific regular expressions for specific purposes and just reuse the regular expression. For example, if the developer wants to search for a term “resource” across the code base without the search being case-sensitive, then the developer could define a regular expression accordingly like the following:

String content = “This is a resource product”;

String patternString = “.*reSoUrCe.”;

Pattern pattern=Pattern.compile (patternString, Pattern.CASE_INSENSITIVE);

Re-usability and efficiency are two of the main advantages of having a regular expression. The great thing is that multiple regular expressions can be defined depending on the purpose.

Must Read – ნაბიჯები მუშაობა ჯავის generics,en?








Components of Java regular expression package

Before we start writing regular expressions in Java, we should have a clear understanding of the components available in the java.util.regex package.

There are mainly three classes available in the package. Let’s have a look at what they actually do.

Pattern class: Regular expressions are generally represented as a sequence of characters/symbols/special characters etc. So, the basic process is to make it an object before it is usable. Once the regular expression is compiled, it becomes a pattern object. Pattern class does not have any public constructor. It has a public static void method called ‘compile’. Regular expression is passed to this method as an argument and the return object is a Pattern object. As we have used in our sample application.

Pattern test_pattern = Pattern.compile (“\\s ”, Pattern.CASE_INSENSITIVE);

Also read – JSON data and Java – How it works?

მეტრი კლასი,,en,ერთხელ შეიქმნა ნიმუში ობიექტი,,en,ჩვენ უნდა გვქონდეს გარკვეული მექანიზმი, შეადაროთ შეყვანის სტრიქონი ნიმუში,,en,აქ მოდის Matcher კლასის როლი,,en,იგი მოქმედებს როგორც რეგექს ძრავა შეესაბამება შეყვანის და ნიმუში,,en,მას არ გააჩნია საჯარო კონსტრუქტორი,,en,საკმაოდ მატრიცული მეთოდი Pattern ობიექტი გამოიყენება შეყვანის და დაბრუნებას Matcher ობიექტი,,en,ამის შემდეგ "მატჩები,,en,Matcher კლასის მეთოდი გამოიყენება ფაქტობრივი შედეგის მისაღებად,,en,ის დააბრუნებს ლოგიკურ ღირებულებას, რაც მიუთითებს თუ არა შედეგი,,en,შესაბამისი,,en,ან ცრუ,,en,არ შეესაბამება,,en,შემდეგი მაგალითი გვიჩვენებს განხორციელებას,,en,ტესტირების Regex კომპონენტები Java- ში,,en,პაკეტი demo.techalpine.com,,en,იმპორტი java.util.regex. *,,ro,საჯარო კლასი RegEx კომპონენტები,,en,შექმნა ნიმუში ობიექტი,,en,ნიმუში tst_pattern = Pattern.compile,,en,დუეტ,,uz,ქმნის Matcher ობიექტი,,en: Now, once the Pattern object is created, we need to have some mechanism to compare the input string with the Pattern. Here comes the role of Matcher class, it acts as a regex engine to match the input and the Pattern. It does not have any public constructor, rather the matcher method of Pattern object is used to take the input and return a Matcher object. After this the ‘matches’ method of Matcher class is used to get the actual result, it returns a Boolean value indicating whether the result is true (matched) or false (not matched).








Following example shows the implementation.

Listing 1: Testing Regex components in Java

[კოდი]

package demo.techalpine.com;

import java.util.regex.*;

public class RegExComponents {

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი[] args) {

//Creating Pattern object

Pattern tst_pattern = Pattern.compile(“.duoni.”);

//Creating Matcher object

მატჩები tst_matcher = tst_pattern.matcher,,sv,შეყვანის სტრიქონის ტესტირება,,en,შემოწმება,,en,თუ შეყვანის სიმებიანი შეესაბამება ნიმუში,,en,tst_matcher.matches,,lb,ახლა შედგენა და აწარმოებს კლასს,,en,PatternSyntaxException კლასი,,en,ეს არის გამონაკლისი კლასი, რომელიც გამოიყენება სინტაქსის შეცდომის გამოვლენისთვის,,en,ახლა თქვენი გარემო იქმნება ჯავაში რეგულარული გამონათქვამების დაწერა,,en,შევეცადოთ ნიმუში განაცხადი,,en,ამ სექციაში ჩვენ დავწერთ ნიმუში განაცხადს რეგულარული გამოხატვის შესამოწმებლად,,en,ეკრანის შემდეგ ნაჩვენები იქნება პროექტის სტრუქტურა,,en,ამ მაგალითში გვექნება მარტივი სტრიქონი თეთრი სივრცეებით,,en,ეს სიმები შეავსებს შეყვანის მაძიებელს,,en,ახლა ობიექტურია თეთრი სივრცის ამოღება და შეცვალოს იგი "**" სიმებიანი,,en,ჩვენ შევქმნით Pattern კლასის და Matcher კლასის,,en(“TechAlpine”);

//Testing input string

System.out.println(“Checking…if input string matches with the pattern – “+tst_matcher.matches());

}

}

[/კოდი]

Now compile and run the class.

PatternSyntaxException class: This is the exception class used for detecting any syntax error.

Environment setup:

To configure the environment following are the four steps to be followed

  • Download JDK from the following link and set the class path

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Now your environment is set to write regular expressions in Java. In the next section we will create one sample application.

You will like to read – Creating RESTful web service in Java

Let’s try a sample application:

In this section we will write a sample application for testing regular expression. Following screen shot shows the project structure for the application.

In this example we will have a simple string with white spaces. This string will bet the input to the matcher. Now the objective is to remove the white spaces and replace it with ‘**’ string.

We will create a Pattern class and a Matcher class. After this the input string will be passed to check with the pattern. We will also display the indexes of the white spaces in the string.

Follow the project structure in the Eclipse environment. Once the program is written, compile and run it.


Listing 2: განცხადების ნიმუში

[კოდი]

package demo.techalpine.com;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RegDemo {

// Creating sample string for testing

public static final String SAMPLE_STRING = “Let us check regular expression in Java”;

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი[] args) {

Pattern test_pattern = Pattern.compile(“\\s ”, Pattern.CASE_INSENSITIVE);

Matcher test_matcher = test_pattern.matcher(SAMPLE_STRING);

//Let us print the matcher

System.out.println(“Printing the matcher:- “+ test_matcher “\N”);

//Let us check matching indexes

System.out.println(“Printing indexes:- “+”\N”);

ხოლო (test_matcher.find()) {

System.out.print(“Starting index: ” + test_matcher.start());

System.out.print(“Closing index: ” + test_matcher.end() + “\N”);

}

//Now compiles the pattern

Pattern replace_pattern = Pattern.compile(“\\s ”);

//Matching the pattern with the sample string

Matcher second_matcher = replace_pattern.matcher(SAMPLE_STRING);

//Printing original string

System.out.println(“\N”+”Printing original string:- “+”\N”);

System.out.println(SAMPLE_STRING);

//Now replace the spaces with ** character

System.out.println(“\N”+”ბეჭდვის შეცვლილი სტრიქონი,,en,second_matcher.replaceAll,,en,ახლა შედგენა და აწარმოებს განცხადებას,,en,ამ სტატიაში ჩვენ განვიხილეთ Java რეგულარული გამოხატვის პაკეტის სხვადასხვა კომპონენტი,,en,ჩვენ ასევე შევამოწმე, როგორ მუშაობს და რატომ არის მნიშვნელოვანი,,en,ჩვეულებრივი გამონათქვამები გამოიყენება სხვადასხვა სცენარებში, როგორიცაა პაროლი დადასტურება,,en,ელექტრონული ფოსტის მისამართი დადასტურება და ა.შ.,,en,ძირითადი იდეა არის ის, რომ ნიმუში თითო მოთხოვნა და შემდეგ დასტური,,en,ეს რეგულარული გამონათქვამები რეალურად გააკეთეს,,en,იგი ამცირებს განვითარების დროს და ქმნის მეორადი კომპონენტებს,,en,ასე რომ, ეს ძალიან სასარგებლოა რეგულარული გამონათქვამების შესწავლაში და შემდეგ საჭიროებისამებრ შეიქმნას,,en,ყველა ჯავის სტატია,,en,ჯავა ზოგადი,,en,რეგულარული გამოხატვა,,en,techalpine.com/learn-regular-expressions-in-java,,en:- “+”\N”);

System.out.println(second_matcher.replaceAll(“**”));

}

}

[/კოდი]

Now compile and run the application.

Read – Learn Log4j – Logging API for Java








დასკვნა: In this article we have discussed different components of Java regular expression package. We have also checked how it works and why it is important. Regular expressions are used in different scenarios like password validation, email address validation etc. The basic idea is to create a pattern as per the requirement and then validate the input. This is what regular expressions actually do. It reduces the development time and creates reusable components. So it is very helpful to learn regular expressions and then create it as per need.

Must Read- All Java articles


 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share