Applications of Regular Expression in Automata



In automata theory and in programming languages as well, regular expressions are a powerful tool for working with text. They are used in applications for validating user input to extracting data from websites.

In this chapter, we will see three common applications of regular expressions (RE's). Here we will see examples for a deeper understanding of how they work in practice.

Let us understand the three major applications of Regular Expressions in detail.

Regular Expression's in UNIX

Let us look at regular expressions in UNIX. UNIX is an operating system like Linux. Regular expressions used in UNIX are extended versions of regular expressions. They allow non-regular languages to be recognized.

Character Classes in UNIX Regular Expressions

UNIX Regular Expressions have specific rules for defining character classes, which are sets of characters that can match a single position in the input string.

Character Classes Description
The Dot Symbol (.) The dot symbol in UNIX RE's is a wildcard that matches any single character.
For example, the expression "a.b" would match "aab", "acb", and "a1b", but not "ab" or "a12b".
Explicit Character Lists You can explicitly list the allowed characters within square brackets. The expression [a1b] matches a single character that can be 'a', '1', or 'b'. This is equivalent to expressing it as "a + 1 + b".
Ranges Using a hyphen (-) within a character class signifies a range.
For example, "[0-9]" matches any single digit from 0 to 9. This range notation simplifies the expression and is equivalent to writing "0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9".
Similarly, "[A-Z]" matches any uppercase letter.
Combined Ranges and Lists You can combine explicit character lists and ranges within the same character class.
For example, "[A-Za-z0-9]" matches any letter (uppercase or lowercase) or digit.
Characters for Signed Digits The character class "[+\-.,0-9]" defines the characters allowed for forming signed digits, including plus (+), minus (-), decimal point (.), comma (,), and digits 0 to 9.

Special Notations

UNIX Regular Expressions provide special notations for commonly used character classes. This simplifies the expressions. These notations helps to make notation easier to maintain consistency within UNIX commands.

Special Notations Description
:digit: The ":digit:" notation represents the same character class as "[0-9]", matching any digit.
:alpha: This notation represents "[A - Za - z]", matching any letter
:alnum: This notation represents the character class "[A-Za-z0-9]", matching any letter or digit.

Unix Operators

UNIX Regular Expressions use specific operators to construct complex patterns.

Operators Description
Pipe (|) The pipe symbol functions as a union operator. It allows for matching one pattern or another. In regular expressions, this is often represented by the plus (+) symbol.
For example, "cat|dog" would match either "cat" or "dog".
Question Mark (?) The question mark indicates "zero or one of" the preceding pattern.
In regular expression notation, this would be equivalent to "ε + R" (epsilon plus R), where R represents the preceding pattern. So, "a?" matches either "a" or an empty string (no "a").
Plus (+) The plus symbol indicates "one or more of" the preceding pattern.
In regular expression notation, this translates to "RR*" (R concatenated with R star), or equivalently "R+". So, "a+" matches "a", "aa", "aaa", and so on.
Braces ({n}) Braces with a number 'n' inside indicate "n copies of" the preceding pattern. For example, "a{5}" matches exactly five consecutive "a" characters ("aaaaa").
This is equivalent to writing "R5" in regular expression notation, where R is the preceding pattern.
Star (*) The star operator, representing "zero or more of" the preceding pattern, continues to be used in UNIX RE's.

Regular Expressions in Lexical Analysis

In compiler design, lexical analysis it breaks down input text into meaningful units called tokens. Regular expressions are widely used in lexical analysis, and text processing.

Consider the following regular expression −

$$\mathrm{'[A \:-\: z][a \:-\: z]^{*}\:' | '\:['A \:-\: Za \:-\: z0 \:-\: 9]^{*}\:' | '\:['A \:-\: Za \:-\: z][A \:-\: Za \:-\: z]'}$$

This expression can be translated into regular expression notation as −

$$\mathrm{(A \:+\: B \:+\: \dotso \:+\: Z)(a \:+\: b \:+\: \dotso \:+\: z)^{*} \:+\: (\varepsilon \:+\: (A \:+\: B \:+\: \dotso \:+\: Z)(a \:+\: b \:+\: \dotso \:+\: z)^{*} \:0 \:+\: 1 \:+\: \dotso \:+\: 9)^{*} \:+\: (ε \:+\: (A \:+\: B \:+\: \dotso \:+\: Z)(A \:+\: B \:+\: \dotso \:+\: Z))}$$

Where "..." represents a blank space.

This regular expression can be used to represent addresses like "Ithaca NY," "Buffalo NY," and so on. It defines three alternative patterns −

Patterns Description
[A - z][a - z]* This pattern matches a word starting with a capital letter followed by zero or more lowercase letters.
This would match "Ithaca" and "Buffalo" in our address examples.
['A - Za - z0 - 9]* This pattern matches an optional alphanumeric string. This would match the empty string before "NY" in our address examples, as well as potential house numbers or street names that might contain numbers.
['A - Za - z][A - Za - z] This pattern matches a two-letter uppercase string, such as "NY" in our examples.

Finding Patterns in Text

Let us look at finding patterns in text. We can use regular expressions for another common application to find specific patterns in text, particularly for tasks like text search and data extraction.

Consider the following incomplete Regular Expression for finding addresses within a web page −

$$\mathrm{'[0 \:-\: 9] \:+\: [\:][A \:-\: Z][a \:-\: z]^{*}[ \:]'|'[A \:-\: Z][a \:-\: z]^{*}[\: ]'|'[A \:-\: Z][a \:-\: z]^{*}[\: ][A \:-\: Z][a \:-\: z]^{*}[ \:]'}$$

'(Street|ST|street|st)'|'(Avenue|AVE|avenue|ave)'|'(Road|RD|road|rd)'|'(Blvd|BLVD|blvd)'

Let us understand the terms −

The numbers and Street Names

The first part of the expression matches different combinations of numbers and street names −

Patterns Description
[0 - 9] + [ ][A - Z][a - z]*[ ] Matches house numbers followed by a street name starting with a capital letter.
[A - Z][a - z]*[ ] Matches street names starting with a capital letter.
[A - Z][a - z]*[ ][A - Z][a - z]*[ ] Matches two-word street names, both starting with capital letters.

The second part matches common street type abbreviations, including "Street", "Avenue", "Road", and "Boulevard", in both uppercase and lowercase variations.

Conclusion

Regular expressions are a useful technique in automata theory as well as in other domains. Applications of regular expressions are diverse, ranging from operating system commands to lexical analysis in compilers and pattern searching etc.

In this chapter, we highlighted the core concepts of UNIX regular expressions, operators, and special notations with examples for a clear understanding on their applications.

Advertisements