Metacharacter

A metacharacter is a character that has a special meaning to a computer program, such as a shell interpreter or a regular expression (regex) engine.

In POSIX extended regular expressions,[1] there are 14 metacharacters that must be escaped (preceded by a backslash ("\")) in order to drop their special meaning and be treated literally inside an expression: opening and closing square brackets ("[" and "]"); backslash ("\"); caret ("^"); dollar sign ("$"); period/full stop/dot ("."); vertical bar/pipe symbol ("|"); question mark ("?"); asterisk ("*"); plus sign ("+"); opening and closing curly brackets/braces ("{" and "}"); and opening and closing parentheses ("(" and ")").[2][3]

For example, to match the arithmetic expression "(1+1)*3=6" with a regex, the correct regex is "\(1\+1\)\*3=6"; otherwise, the parentheses, plus sign, and asterisk will have special meanings.

Other examples

Some other characters may have special meaning in some environments.

Escaping

The term "to escape a metacharacter" means to make the metacharacter ineffective (to strip it of its special meaning), causing it to have its literal meaning. For example, in PCRE, a dot (".") stands for any single character. The regular expression "A.C" will match "ABC", "A3C", or even "A C". However, if the "." is escaped, it will lose its meaning as a metacharacter and will be interpreted literally as ".", causing the regular expression "A\.C" to only match the string "A.C".

The usual way to escape a character in a regex and elsewhere is by prefixing it with a backslash ("\"). Other environments may employ different methods, like MS-DOS/Windows Command Prompt, where a caret ("^") is used instead.[5]

gollark: An actual employee? No. We'll use HTech™ Personality Constructs™.
gollark: Also, to help with sleep monitoring, it will ship with an optional EEG headset.
gollark: A what? No, this is the osmarksßßsmartwatch™.
gollark: Anyway, the osmarksßßsmartwatch™ will also incorporate the latest sensor technology, like an accelerometer, a compass for some reason also, a thermometer, a barometer, a humidity sensor, a light level/UV/IR sensor, an ultrasonic distance sensor, a regular microphone, an irregular microphone, lidar, radar, an infrared thing, two incompatible software defined radios, that one weird IC some company made for some reason to detect lightning strikes nearby, a spectrometer, LEDs abused as photodetectors, a DVD player (DVDs must be shrunken or trimmed before use), a portable DNA sequencer, a multi-axis Hall effect sensor, phased array satellite transceivers, atmospheric bismuth concentration meters, an apiometer, a mouse trackball, an optical mouse (miniaturized), a full 22-key keyboard, 3 dedicated hardware buttons, a fan noise detector and estimator, and a blood oxygen concentration reader.
gollark: We'll send them cardboard models.

See also

References

  1. "The Open Group Base Specifications Issue 6: Ch. 9  Regular Expressions". pubs.opengroup.org. The Open Group. 2004. Retrieved 2018-11-19.
  2. Goyvaerts, Jan (December 8, 2016). "Regular Expressions Quick Start". www.regular-expressions.info. Retrieved 2018-11-19.
  3. Nield, Thomas (December 13, 2017). "An introduction to regular expressions". O'Reilly Media. Retrieved 2018-11-19.
  4. "Character entity references in HTML 4". www.w3.org. W3C. December 24, 1999. Retrieved 2018-11-19.
  5. "Command shell overview". docs.microsoft.com. Microsoft. September 10, 2009. Retrieved 2018-11-19.
  6. "The Open Group Base Specifications Issue 7: fprintf". pubs.opengroup.org. The Open Group. 2018. Retrieved 2018-11-19.
  7. "LIKE (Transact-SQL)". docs.microsoft.com. Microsoft. March 14, 2017. Retrieved 2018-11-19.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.