Introducing regular expression

The use of regular expressions is a widespread approach that is used in search engines and text-processing utilities. Many programming languages support regexes out-of-the-box or use libraries. Good examples of such usage include the Find and Find in path actions in IntelliJ IDEA, which can be used to find code. The Find in path window looks as follows:

To find a sequence of characters using regular expressions, we have to provide a pattern that consists of special characters; these are listed as follows:

Subexpression Matches
^ The search sequence of characters must start from the beginning of the line.
$ The search sequence of characters must be at the end of the line.
. The search character may be any except the new line. The m option allows you to find the new line character as well.
[...] The search character may be any one in brackets.
[^...] The search character may be any one without brackets.
A The search sequence of characters must be at the beginning of the entire string.
z The search sequence of characters must be at the end of the entire string.
 The search sequence of characters must be at the end of the entire string, if it doesn't contain a final-line terminator.
* This means that the previous expression may occur any number of times, or not at all.
re+ This means that the previous expression may occur at least once.
re? This means that the previous expression may occur any number of times, or not at all.
re{ n} This means that the previous expression may occur n times. 
re{ n,} This means that the previous expression may occur n or more times. 
re{ n, m} This means that the previous expression may occur at least n and at most m times.
a|b This means that a pattern matches a character, a or b.
(re) This means that r and e characters group into one expression and remember the matched text.
(?: re) This means that r and e characters group into one expression without remembering the matched text.
(?> re) This refers to the independent pattern without using remembering.
w This matches a word.
W This matches if a piece of text doesn't contain words.
s This matches a white space and equals [ f].
S This doesn't match a white space.
d This matches a digit and equals [0-9].
D This doesn't match a digit.
G This can be used to define a point where the last match finished.
This refers to a back-reference to capture the group number, n.
 This matches the backspace when it is inside brackets.
B This doesn't match word boundaries.
, , and more. This matches special symbols such as newlines, carriage returns, and tabs.
Q This escapes all characters up to E.
E This refers to end escaping.

 

Using the ^ symbol, we can establish that we can find a sequence of characters from the start of the string, and we can use $ symbol in a similar way but for the end of the string. For instance, the following regular expression means that we want to find The at the beginning of the line:

^The 

We can also find the following example for the end of the line:

end$

We can combine subexpressions to create a more complex pattern:

^The end$

In a similar way, the . symbol matches any single character, and d matches any digit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.204