Introduction to Regular Expressions

From Apache OpenOffice Wiki
Jump to: navigation, search

Editing.png This page is in a DRAFT stage.


We already mentioned what Regular Expressions are. With Regular Expressions (from now, RegExp), you can "match" non fixed text strings. For example, the expression


will match any digit,


will match an arbitrary integer and


will match an arbitrary number that may (or may not) have a decimal part separated by a dot.

RegExp expressions are case-insensitive by default, that is, they match upper and lower case letters alike. You enable case-sensitivity by using the „Match case“ check box.

As it is possible to see from these examples, on RegExp several characters have a particular meaning. On this article the main characteristics of the ICU based RegExp engine used by Apache OpenOffice will be commented.

Special Characters

As previously mentioned, several characters have a particular meaning inside regular expressions. These characters are all kind of brackets, the backslash, the dot, the dollar sign, etcetera. In order to search for those special characters we need to "escape" them with a backslash: for example, to search for [ it is needed to write


and of course, in order to search for the backslash, two backslashes must be written


The Dot

The dot can be used to match a generic character.


will match dat, dot, dut... even dXt.

The expression


is equivalent to the dot.

Question Mark

Used to match zero or one instances of the previous character. For example


will match either mat or math.

The Plus

Used to match one or more instances.


Will match math, mathh, mathhh...

The Asterisk

Match an arbitrary number of instances, even zero:


will match mat, math, mathhhhhh...

Curly Bracket

They can be used to set the number of times the previous character must be repeated. For example


will match ouuch!, ouuuch!, ouuuuch!, ouuuuuch!, but not ouch!

Using only one number will match the exact number


Will only find ouuuch!, while


Will find the word with at least three u.


The ^ sign have several meanings depending on the context. For one part, it can be used to match anything at the beginning of a paragraph. For example, in a paragraph like

  The text on the book

the expression


will match the first The, but not the second.

Inside square brackets, the ^ is used to negate a character. For example,


will match any character that is not an a, while


will match a character that is not either an a nor a r.

The Dollar Sign $

This symbol has different meanings on different contexts too.

Used alone, it find paragraph breaks, but used with other characters it will find those characters at the end of the paragraph. For example, on the paragraph

  Your book is under this book

the expression


will find the second instance of book, but not the first one. On the Replace by box, the $ sign has a different meaning, that we will see below.

The Backslash \

As mentioned before, the backslash can be used to escape special symbols in order to search for them, but it can also give a different meaning to normal characters. For example,


will search for a word boundary:


will match the first three characters on just, justice, justly... but not adjust, while


will find the last three characters on common, summon... but not monitor.

The expression


will find any character inside a word.


(with a cap W) will find a character that is not a "word element" (a space, a question mark... etcetera)


a tab stop


will find a line break, but on the replace box will insert a paragraph break (the same you find with $).

The combination \u followed by an hexadecimal number can be used to search for specific characters with its Unicode code point. For example


will find the Greek character delta δ

The Vertical Bar |

This can be used to set alternatives. For example


will find either mote or more. This can be used on expressions with more options, like [a|b|c].

Groups and References

Grouping expressions with parenthesis, like


gives the possibility to call each expression later on the RegExp formula with a backslash followed by a number: \1 will call the first expression, \2 the second, etcetera.

For example, the regular expression

  \b(\w+) *\1\b

(note the space between (w+) and the *) can be used to find repeated words: both \b expressions find a word boundary, \w+ find one or more word elements while the \1 call the same elements found on with the group (\w+).

Documentation note.png An alternative that also searches for duplicated words with a punctuation sign between them could be



Note that the first expression will fail on AOO 3.4.1 because of a bug solved on 4.0.

In order to call the text found by the group on the Replace by box, you need to use the dollar sign followed by the expression number instead of the backslash.

For example, searching by

  \b(\w+) *\1\b

and replacing by


will eliminate any duplicated word on the text.

Other Expressions


will find any ASCII character, but not accented character nor numbers, for that you need to use


or the dot, as seen before.




will find a single digit.


will find any kind of space, even non-breaking ones.

It is possible to use the dash together with the square brackets to set ranges. For example,


will find any digit but 9.

On the replace box,


will insert the same string found with the Search RegExp.

Limits on the Use of Regular Expressions

RegExp can only search inside a paragraph: you cannot use them for example to find two paragraphs looking for a particular end on the first one and a particular beginning on the second one.

Paragraph marks cannot be found in combination with other text. For example, on a paragraph like

 A short example. This is the end of the text.

the expression


will only find final dot, not the paragraph break.

Beside the already indicated expressions (&, \n, the call for groups with the dollar sign followed by a number), the "Replace by" box does not accept regular expression.

Regular Expressions on Writer

On Writer, going to Edit → Find & Replace will open the Find & Replace menu. There, with a click on  More Options  you'll find a checkbox to enable the RegExp tool:


As you can see from the screenshot, it is possible to combine RegExp with other options like  Format .

Regular Expressions on Calc


Search and Replace with Search Dialogue


Regular Expressions on Calc Formulas


Regular Expressions on Impress


More information

Personal tools