The name of Regex is derived from Regular Expression. The regex or regular expression will be a series of various characters which explains a specific pattern of search.It is shown as Rational expression mainly for searching and managing text strings. It is easily used to search a pattern which we can replace with the matching pattern.
This tool is made use in all the programming languages as well as scripting language like PHP, C, C++, Java, Perl and javaScript, ruby and python. It is mainly used as word processors like word helps the clients users who searches the text in a document.
Regular Expression characters:
The following are the different type of characters of a regular expressions
- Metacharacters
- Quantifiers
- groups and ranges
- Escape characters
The Metacharacters are:
- ^ : This character is used to match a expression that is right at the starting of any string. For example ^a will be an expression which matches the string that starts with ‘a’ such as “aab”
- $: This sign is employed to match an expression to its left at the end of the string. For example r$ is an expression to match to a string which will end with an symbol r like “aaabr”, ”ar”, ”r”.
- . : This is the character that is employed to match any single character string without the terminator line as /n. For example b.x will be an expression that will match strings like “bax”, ”bar”.
- | : It is a character matched for a particular group of characters on either side. If the character is on the left side that is matched then the right side character will be ignored. For example A|B is an expression that will give many strings but all the strings contain either a or b.
- \ : It is used specifically to escape the special character after this sign in a string.
Quantifiers
The quantifiers are used for all regular expressions for telling the number of occurrences of a character.
The quantifiers
- + -: This character will be specifying an expression to the left for more times. For example s+ is an expression which gives “s” ,”s”.
- ? -: This character mainly specifies an expression to its left for 0 or 1. For example aS? is called an expression which gives either “a”.
- * -: This character will make out an expression to the left for 0 or more times. For example Br* will be an expression which gives “B”,”Brrr” etc.
- {x} -: It mentions an expression to its left for only x times. For example Mab{5} will be an expression which provides the following string which contains 5 b’s Mabbbbb.
- {x,} -: It says an expression to the left side for x or more times. For example Xb{3,} is an expression which gives various strings containing at least 3 b’s. Such strings as “Xbbb” and so on.
- {x,y} -: It will say an expression to its left at least x times but less than y times. Consider an example, Pr{3,6} a is an expression which provides two strings Both strings will as follows “Prrrr”.
Escape characters:
- \s –It is employed to match a one white space character.
- \S –It matches one white space character.
- \0 –It matches Null character.
- \a –It matches bell or alarm
- \d –It will be used to match one decimal digit ,which ,means 0 to 9.
- \D –It matches any decimal digit.
- \w –It matches the alphanumeric characters.
- \W –It matches one non-word character.
Regular expressions in Java language:
In Java language, Regex will be an expression for application programming interface which is used for manipulating, searching and editing a string. We have subsequent classes which comes under the java.util.regex package
- regex.pattern- util.regex.pattern this class will be compiled version of Regex and called by the compile() method. The compile() method that accepts the regex as a first argument. This class does not have public constructor.
Example
import java.util.regex.*;
class reg{
public static void main(String args[]){
System.out.println(Pattern.matches(“.r.”, “app”)); // This line displays Boolean value True because the second character is r in both string and regular exp.
System.out.println(Pattern.matches(“.bm”, “abc”)); //here the Boolean value is false as the third character will be different in regular expression and strings
System.out.println(Pattern.matches(“..m”, “mnm”)); //This line displays Boolean value True as the third character is m in string and regular exp.
System.out.println(Pattern.matches(“a..s”, “asns”)); //This line displays Boolean value True because the first and last character is same in both string and regular exp.
System.out.println(Pattern.matches(“.s.”, “mds”)); //Line displays Boolean value False because the second character is different in both string and regular exp.
}
}
Questions
1. What is Regex?
2. What are the advantages of Regex?