go to previous page   go to home page   go to next page



Back References

Regular Expression
Group 0
Group 1 Group 2 Group 3 Group 4

An expression can ask to match the exact characters that a capture group has captured. In an expression, \1 means whatever characters are currently in group number 1. For example

(ant|bat|cat) \1

first matches ant or bat or cat, matches a space, and then matches whichever of the three animals started the string.

It matches ant ant or bat bat or cat cat but not ant bat nor similar strings.

The \1 is called a back reference or sometimes called a variable. Other variables are \2 , \3 , and so on. For example

(\w+) (\w+) \w+ \2 \1


fall leaves after leaves fall 


Write an expression that matches strings that have an 'X' in the middle and an equal number of 'a' on either side of the 'X'.

For example, aaaaXaaaa and X and aaaaaaaXaaaaaaa are strings that match.