Skip to main content

Character sets

You can define a set of characters within a string. Character sets match any single character enclosed in square brackets [ ]. For example:

Expression

Match

j[oa]n

jon or jan

Use the - character between two characters in a set to indicate a range. A - character outside of a set, or at the beginning or end of a set, is treated literally. For example:

Expression

Match

[a-c]

a , b , or c

[1-3]

1 , 2 , or 3

[ab-]

a , b , or -

[B-D]-3

B-3 , C-3 , or D-3

If the first character in the set is ^, then the character set matches any character that is not in the list. For example:

Expression

Match

[^oa]

Any character except o and a

All other special characters lose their meaning when included in a character set. For example:

Expression

Match

^[^*\+.-]

Line must not start with *, \ , + , . ,or -

To include a literal ] in the character set, make it the first character in the set. It can also be excluded by following ^. The ] character is also treated literally outside of a set. For example:

Expression

Match

[^]a-]

Any character besides ] , a , and -

[]a]

] or a

[a]]

a]