What is Regular expression?

A notation that expresses a pattern of strings by combining symbols. . means any single character, * zero or more repetitions of the preceding element, ^ and $ the start and end of a line, and [abc] any one of the characters in the brackets; regular expressions are used for searching and replacing in commands such as grep, sed and awk and in many programming languages. grep's basic regular expressions (BRE) and extended regular expressions (grep -E, ERE), in which +, ? and | can be used directly, treat some symbols differently. They are different from shell wildcards (such as *.txt), and * has a different meaning.

Japanese name: 正規表現

Regular expression appears in 4 practice questions across 2 topics, including Compilers, Linux and Command-Line Basics.

Field: Linux and Command-Line Basics

Try it: questions about Regular expression

What is the role of lexical analysis in a compiler?

  1. AChecking, in tree form, whether the program's statements follow the grammar
  2. BSplitting the characters of source code into tokens such as identifiers and operators
  3. CChecking that variable types match and that no undefined names are used
Show answer and explanation

Answer: B. Splitting the characters of source code into tokens such as identifiers and operators

Lexical analysis is the first phase of a compiler. It splits, for example, "count = count + 1;" into the tokens identifier count, symbol =, identifier count, symbol +, number 1 and symbol ;. Whitespace and comments are discarded here. Token rules can be written as regular expressions, so a finite automaton processes them quickly. Checking grammar in tree form is the next phase, parsing, and checking types and names is semantic analysis.

What is commonly used to express the rules for recognizing tokens in lexical analysis?

  1. AFloating-point rounding rules
  2. BRegular expressions, processed by finite automata
  3. CCache replacement policies
Show answer and explanation

Answer: B. Regular expressions, processed by finite automata

Token types can be written concisely as regular expressions, such as "a letter followed by letters or digits" for identifiers and "one or more digits" for numbers. Regular expressions convert to finite automata, machines with a finite number of states that change state with each character read, so tokens are extracted in time proportional to input length. Lexers are therefore often generated automatically from regular expressions by tools. Rounding and cache rules have nothing to do with lexical analysis.

Regular expression in the explanations

Questions that mention Regular expression (4)

  1. What is the role of lexical analysis in a compiler?
  2. What is commonly used to express the rules for recognizing tokens in lexical analysis?
  3. Which kind of grammar is used to describe most programming languages and underlies parsing?
  4. In the session above, which of the following are the lines printed by the last grep?

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Lexical analysis Syntax analysis (parsing) grep cron Environment variable File permissions Git Hard link I/O redirection inode

Back to the glossary