# Regular Expressions

jEdit uses regular expressions from java.util.regex.Pattern to implement inexact search and replace. Click there to see a complete reference guide to all supported meta-characters.

A regular expression consists of a string where some characters are given special meaning with regard to pattern matching.

## Inside XML files

Inside XML files (such as jEdit mode files), it is important that you escape XML special characters, such as &, <, >, etc. You can use the XML plugin's "characters to entities" to perform this mapping.

## Inside Java / beanshell / properties files

Java strings are always parsed by java before they are processed by the regular expression engine, so you must make sure that backslashes are escaped by an extra backslash (`\\`)

Within a regular expression, the following characters have special meaning:

Positional Operators

- `^` matches at the beginning of a line

- `$` matches at the end of a line

- `\b` matches at a word boundary

- `\B` matches at a non-word break

- `\A` The beginning of the input

- `\G` The end of the previous match

- `\Z` The end of the input but for the final terminator, if any

- `\z` The end of the input

One-Character Operators

- `.` matches any single character (may or may not match line terminators)

- `\d` matches any decimal digit (`[0-9]`)

- `\D` matches any non-digit (`[^0-9]`)

- `\n` matches the newline character (`\u000A`)

- `\s` matches any whitespace character (`[ \t\n\x0B\f\r]`)

- `\xhh` matches hexadecimal character code `0xhh`

- `\S` matches any non-whitespace character (`[^\s]`)

- `\t` matches a horizontal tab character (`\u0009`)

- `\w` matches any word character (`[a-zA-Z_0-9]`)

- `\W` matches any non-word character (`[^\w]`)

- `\\` matches the backslash character (\)

Character Class Operator

- `[abc]` matches any character in the set a, b or c. A leading ] will be interpreted literally.

- `[^abc]` matches any character not in the set a, b or c. A leading ] after the ^ will be interpreted literally.

- `[a-zA-Z]` matches any character in the ranges a to z and A to Z, inclusive. A leading or trailing dash and a leading ] will be interpreted literally.

Subexpressions and Backreferences

- `(abc)` matches whatever the expression abc would match, and saves it as a subexpression. Also used for grouping

- `(?<name>abc)` matches whatever the expression abc would match, and saves it as a subexpression called name. Also used for grouping

- `(?:...)` pure grouping operator, does not save contents

- `(?=...)` positive lookahead; the regular expression will match if the text in the brackets matches, but that text will not be considered part of the match

- `(?!...)` negative lookahead; the regular expression will match if the text in the brackets does not match, and that text will not be considered part of the match

- `(?<=...)` positive lookbehind; the regular expression will match if the text in the brackets matches, but that text will not be considered part of the match

- `(?<!...)` negative lookbehind; the regular expression will match if the text in the brackets does not match, and that text will not be considered part of the match

- `(?>...)` pure possessive grouping operator, does not save contents and does not back off during backtracking

- `\n` where 0 < n < 10, matches the same thing the nth subexpression matched. Can only be used in the search string

- `$n` where 0 < n < 10, substituted with the text matched by the nth subexpression. Can only be used in the replacement string

- `\k<name>`, matches the same thing the subexpression called name matched. Can only be used in the search string

- `${name}`, substituted with the text matched by the subexpression called name. Can only be used in the replacement string

Branching (Alternation) Operator

- `a|b` matches whatever the expression a would match, or whatever the expression b would match.

Repeating Operators

These symbols operate on the previous atomic expression.

- `?` matches the preceding expression once or not at all

- `*` matches the preceding expression zero or more times

- `+` matches the preceding expression one or more times

- `{n}`matches the preceding expression exactly n times

- `{n,m}` matches the preceding expression between n and m times, inclusive

- `{n,}` matches the preceding expression n or more times

Greedy, Reluctant and Possessive Matching

If a repeating operator (above) is immediately followed by a `?`, it behaves reluctant, that is the repeating operator will stop at the smallest number of repetitions that can complete the rest of the match.

If a repeating operator (above) is immediately followed by a `+`, it behaves possessive, that is the repeating operator will match as much characters as it can and will not back off during backtracking, even if that would allow to complete the rest of the match.

If a repeating operator (above) is not immediately followed by a `?` or `+`, it behaves greedy, that is the repeating operator will match as much characters as it can but it will back off character by character during backtracking, if that would allow to complete the rest of the match.

## On regex search

There are some known issues with the `java.util.regex` library, as it stands in Java. In particular, it is possible to create regular expressions that hang the JVM, or cause stack overflow errors, which was not as easy to accomplish using the legacy `gnu.regexp` library. If you find that `gnu.regexp`, used in jEdit 4.2 and earlier, is more suitable for your search/replace needs, you can try the XSearch plugin, which still uses it and can provide a replacement to the built-in search dialog.