Regular expressions in Java Assignment Help

Assignment Help: >> Java Programming Concepts >> Regular expressions in Java

Regular expressions in Java:

All the queries listed as bullet points in can be expressed in terms of Java if statements. However, as a query gets more and more complicated the logic of the if statements becomes tortuous and hence more error-prone. Regular expressions were invented to provide a more compact representation of a string that needs to be searched for. They have been developed in order to reduce the number of if statements necessary in a complex search to just one.

Many software systems provide regular expressions for searching; they can be found in Windows Explorer, word processors, many programming languages and in some browsers.

An example of the simplest regular expression that you can specify is: "The fox saw the duck" This is a simple string that just matches the substring "The fox saw the duck" within any other string. This regular expression is not hugely useful; it is the regular expression version of zero. A more useful regular expression is

Cat*

What this regular expression describes is any string consisting of the substring "Ca" followed by zero or more t characters. This means that it identifies strings such as:

Ca

Catt

Cattttttt

Catttttttttttttttttttttt

The first string is the simplest case, since it matches "Ca". You may have met a form of regular expressions before, in that when you look for files in a platform such as Windows or Linux you might have used the * character to search for files that contain a number of specific characters.

If you want to match a substring that consists of one or more characters then the + symbol is used:

Cat+

will match any string that starts with "Ca" and is followed by one or more t characters.

You can also specify ‘or' options within a regular expression. For example, the regular expression:

A*|b+

will match zero or more capital A characters or one or more b characters.

Brackets can be used to designate substrings. For example, the regular expression: (ab)*c matches any string that consists of zero or more occurrences of the substring "ab" followed by the character c. Thus it will, for example, match:

c  abc  abababc

You can also specify character ranges by using square brackets. For example, the regular expression:

[0-4]*[6-8]*

describes a string that consists of zero or more digits from 0 to 4, followed by zero or more digits from 6 to 8. Thus all the following will be matched:

01 033333333 3332222266887 0000111114444444

Alphabetic characters can also be used within regular expressions in the same way that digits are used. For example, the regular expression:

[a-d]*8*

describes any sequence (including zero) of lower case a, b, c, d followed by zero or more 8 characters; so, for example, the strings:

a aaa88 abcd888888 a8 88 aaaaaaaabbbbbcc abcdabcdabcd will match the regular expression.


A regular expression can also specify that a particular pattern must be recognized a number of times. This is achieved by means of curly brackets. The regular expression:

B{2}

recognizes two capital letter Bs, while: B{2,} matches a string that consists of two or more capital letter Bs. The regular expression B{2,5} matches a string consisting of between 2 and 5 capital Bs.

These are just a few of the facilities that regular expressions offer. Virtually all programming languages offer facilities for specifying regular expressions and applying them to a string in order to discover whether the string consists of particular sub- expression(s) that can be found in the string. Some languages such as Perl, which were defined for string processing, offer extensive facilities for regular expression manipulation, while other languages offer fewer facilities; however, whatever the facility offered, the basic syntax of regular expressions follows the form detailed in this unit.

In Java, regular expression processing is implemented as part of the standard Java libraries. There are also a number of third-party libraries available. The one that I will use in this unit is an API, produced by the Apache Foundation (the Open Source organization that is famous for developing and maintaining the Apache Web server). The API is known as Regexp.

Regexp is a relatively simple API to use. It is configured around a class RE, which defines regular expressions. There is a regular expression library as part of the standard Java distribution but I find the Regexp library easier to use.

An example of the type of coding that is involved when using Regexp is shown below:

RE reg = new RE("x+y+z+");
if(reg.match("abxxyzzzzhello"))
...

Here the regular expression object reg is created and then the string:

"abxxyzzzzhello"

is checked against this regular expression.

The string matches the regular expression since it contains a substring "xxyzzzz" consisting of one or more x characters followed by one or more y characters and then one or more z characters.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Regular expressions in Java homework and assignments? Live Regular expressions in Java experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Regular expressions in Java homework help, java assignment help and Regular expressions in Java projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd