In a regular expression, which characters need escaping?

LanceBaynes

In general, which characters in a regular expression need escaping?

For example, the following is not syntactically correct:

echo '[]' | grep '[]'
grep: Unmatched [ or [^

This, however, is syntatically correct:

echo '[]' | grep '\[]'
[]

Is there any documentation on which characters should be escaped in a regular expression, and which should not?

Matteo

This depends on the application. In your example [ must be quoted as an argument for grep but not echo.

For the shell (from the POSIX specs):

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to preserve the literal meaning of the special characters in the next paragraph, prevent reserved words from being recognized as such, and prevent parameter expansion and command substitution within here-document processing (see Here-Document).

The application shall quote the following characters if they are to represent themselves:

|  &  ;  <  >  (  )  $  `  \  "  '  <space>  <tab>  <newline>

and the following may need to be quoted under certain circumstances. That is, these characters may be special depending on conditions described elsewhere in this volume of IEEE Std 1003.1-2001:

*   ?   [   #   ˜   =   %

The various quoting mechanisms are the escape character, single-quotes, and double-quotes. The here-document represents another form of quoting; see Here-Document.

Specific programs (using regexes, perl, awk) could have additional requirements on escaping.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Which special characters need escaping in a solr query?

From Dev

Regular expression which allows all characters except for "<>_ ;{}[]"

From Java

Escaping plus in sed regular expression

From Dev

regular expression in javascript escaping backslash

From Dev

Escaping characters in Mule Expression Language

From Dev

Why do certain characters need to be escaped in this Elixir regular expression?

From Dev

regular expression which should allow limited special characters

From Dev

Regular expression of validating text which contains certain special characters on condition

From Dev

Escaping special characters like ) in regular expressions in Python

From Dev

Need Grouping of Regular Expression

From Dev

Need a modification in the regular expression

From Dev

Need PHP regular expression

From Dev

Need regular expression expertise

From Dev

Need a modification in the regular expression

From Dev

Need help in this Regular Expression

From Dev

Need assistance with a regular expression

From Dev

Accented characters and regular expression

From Dev

Regular Expression for special characters

From Dev

Need for help in this scrapy regular expression

From Dev

Need a breakdown of the following regular expression

From Dev

Java Regular Expression; need to map \' to ''

From Dev

Need help for writing regular expression

From Dev

Need Number validation regular expression

From Dev

Escape regular expression reserved characters?

From Dev

regular expression to find until characters?

From Dev

Javascript regular expression to capture characters

From Dev

Special Characters in JFlex Regular Expression

From Dev

Writing a regular expression with special characters

From Dev

Regular expression to parse escape characters

Related Related

  1. 1

    Which special characters need escaping in a solr query?

  2. 2

    Regular expression which allows all characters except for "<>_ ;{}[]"

  3. 3

    Escaping plus in sed regular expression

  4. 4

    regular expression in javascript escaping backslash

  5. 5

    Escaping characters in Mule Expression Language

  6. 6

    Why do certain characters need to be escaped in this Elixir regular expression?

  7. 7

    regular expression which should allow limited special characters

  8. 8

    Regular expression of validating text which contains certain special characters on condition

  9. 9

    Escaping special characters like ) in regular expressions in Python

  10. 10

    Need Grouping of Regular Expression

  11. 11

    Need a modification in the regular expression

  12. 12

    Need PHP regular expression

  13. 13

    Need regular expression expertise

  14. 14

    Need a modification in the regular expression

  15. 15

    Need help in this Regular Expression

  16. 16

    Need assistance with a regular expression

  17. 17

    Accented characters and regular expression

  18. 18

    Regular Expression for special characters

  19. 19

    Need for help in this scrapy regular expression

  20. 20

    Need a breakdown of the following regular expression

  21. 21

    Java Regular Expression; need to map \' to ''

  22. 22

    Need help for writing regular expression

  23. 23

    Need Number validation regular expression

  24. 24

    Escape regular expression reserved characters?

  25. 25

    regular expression to find until characters?

  26. 26

    Javascript regular expression to capture characters

  27. 27

    Special Characters in JFlex Regular Expression

  28. 28

    Writing a regular expression with special characters

  29. 29

    Regular expression to parse escape characters

HotTag

Archive