POSIX bracket expressions are a specialized type of character class used in regular expressions. Like standard character classes, they match a single character from a specified set of characters. However, they offer additional features such as locale support and unique character classes that aren't found in other regex flavors.
Key Differences Between POSIX Bracket Expressions and Standard Character Classes
POSIX bracket expressions are enclosed in square brackets ([]
), just like regular character classes. However, there are some important differences:
-
No Escape Sequences: In POSIX bracket expressions, the backslash (
\
) is not treated as a metacharacter. This means that characters like\d
or\w
are interpreted as literal characters rather than shorthand classes.For example:
-
[\d]
in a POSIX bracket expression matches either a backslash (\
) or the letterd
. -
In most other regex flavors,
[\d]
matches a digit.
-
-
Special Characters:
-
To match a closing bracket (
]
), place it immediately after the opening bracket or negating caret (^
). -
To match a hyphen (
-
), place it at the beginning or end of the expression. -
To match a caret (
^
), place it anywhere except immediately after the opening bracket.
-
To match a closing bracket (
Here’s an example of a POSIX bracket expression that matches various special characters:
[]\d^-]
This expression matches any of the following characters: ]
, \
, d
, ^
, or -
.
POSIX Character Classes
POSIX defines a set of character classes that represent specific groups of characters. These classes adapt to the locale settings of the user or application, making them useful for handling different languages and cultural conventions.
Common POSIX Character Classes and Their Equivalents
POSIX Class | Description | ASCII Equivalent | Unicode Equivalent | Shorthand (if any) | Java Equivalent |
---|---|---|---|---|---|
[:alnum:]
|
Alphanumeric characters |
[a-zA-Z0-9]
|
[\p{L&}\p{Nd}]
|
\p{Alnum}
|
|
[:alpha:]
|
Alphabetic characters |
[a-zA-Z]
|
\p{L&}
|
\p{Alpha}
|
|
[:ascii:]
|
ASCII characters |
[\x00-\x7F]
|
\p{InBasicLatin}
|
\p{ASCII}
|
|
[:blank:]
|
Space and tab characters |
[ \t]
|
[\p{Zs}\t]
|
\p{Blank}
|
|
[:cntrl:]
|
Control characters |
[\x00-\x1F\x7F]
|
\p{Cc}
|
\p{Cntrl}
|
|
[:digit:]
|
Digits |
[0-9]
|
\p{Nd}
|
\d
|
\p{Digit}
|
[:graph:]
|
Visible characters |
[\x21-\x7E]
|
[^\p{Z}\p{C}]
|
\p{Graph}
|
|
[:lower:]
|
Lowercase letters |
[a-z]
|
\p{Ll}
|
\p{Lower}
|
|
[:print:]
|
Visible characters, including spaces |
[\x20-\x7E]
|
\P{C}
|
\p{Print}
|
|
[:punct:]
|
Punctuation and symbols |
[!"#$%&'()*+,\-./:;<=>?@[\\\]^_ {
|
}~]` |
[\p{P}\p{S}]
|
|
[:space:]
|
Whitespace characters, including line breaks |
[ \t\r\n\v\f]
|
[\p{Z}\t\r\n\v\f]
|
\s
|
\p{Space}
|
[:upper:]
|
Uppercase letters |
[A-Z]
|
\p{Lu}
|
\p{Upper}
|
|
[:word:]
|
Word characters (letters, digits, underscores) |
[A-Za-z0-9_]
|
[\p{L}\p{N}\p{Pc}]
|
\w
|
|
[:xdigit:]
|
Hexadecimal digits |
[A-Fa-f0-9]
|
[A-Fa-f0-9]
|
\p{XDigit}
|
Using POSIX Bracket Expressions with Negation
You can negate POSIX bracket expressions by placing a caret (^
) immediately after the opening bracket. For example:
[^x-z[:digit:]]
This pattern matches any character except x
, y
, z
, or a digit.
Collating Sequences in POSIX Locales
A collating sequence defines how certain characters or character combinations should be treated as a single unit when sorting. For example, in Spanish, the sequence "ll" is treated as a single letter that falls between "l" and "m".
To use a collating sequence in a regex, enclose it in double square brackets:
[[.span-ll.]]
For example, the pattern:
torti[[.span-ll.]]a
Matches "tortilla" in a Spanish locale.
However, collating sequences are rarely supported outside of fully POSIX-compliant regex engines. Even within POSIX engines, the locale must be set correctly for the sequence to be recognized.
Character Equivalents in POSIX Locales
Character equivalents are another feature of POSIX locales that treat certain characters as interchangeable for sorting purposes. For example, in French:
-
é
,è
, andê
are treated as equivalent toe
. - The word "élève" would come before "être" and "événement" in alphabetical order.
To use character equivalents in a regex, use the following syntax:
[[=e=]]
For example:
[[=e=]]xam
Matches any of "exam", "éxam", "èxam", or "êxam" in a French locale.
Best Practices for POSIX Bracket Expressions
- Know your regex engine: Not all engines fully support POSIX bracket expressions, collating sequences, or character equivalents.
- Be careful with negation: Make sure you understand how to negate POSIX bracket expressions to avoid unexpected matches.
- Use locale settings appropriately: POSIX bracket expressions adapt to the locale, making them useful for multilingual text processing.
POSIX bracket expressions extend the functionality of traditional character classes by adding locale-specific character handling, collating sequences, and character equivalents. These features are particularly useful for handling text in different languages and cultural contexts.
However, due to limited support in many regex engines, it's important to understand your tool’s capabilities before relying on these features. If your regex engine doesn’t fully support POSIX bracket expressions, consider using Unicode properties and scripts as an alternative.
Recommended Comments
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.