Shell notes: regular expressions

Wildcards are used to match qualified file names (exact matches), but regular expressions are used to match qualified strings in files (including matches).


Basic regular expressions

  • *: The previous character matches 0 times or any number of times.
  • .: Match any character except newline.
  • ^: Match the beginning of the line.
  • $: Match the end of the line.
  • []: Match any character specified in square brackets (match only one character),-can be used to represent range characters, such as 0-9 for numbers from 0 to 9, and az for lowercase letters a to z
  • [^]: Match any character except those specified in square brackets. For example, [^ 0-9] means match any character except the digits from 0 to 9, that is, non-numeric characters.
  • \: The escape character can cancel the special meaning of the special symbol in the regular expression and match the special symbol itself.
  • \ {n \}: Match the preceding character n times, such as "[0-9] \ {4 \}" means match 4 digits.
  • \ {n, \}: Match the preceding character at least n times.
  • \ {n, m \}: Match the preceding character at least n times and at most m times.

Common usage:

  • . *: Match any string.
  • ^ $: Indicates matching blank lines.

 

Guess you like

Origin www.cnblogs.com/guyuyun/p/12735085.html