Using Regular Expressions
There are 2 different ways in which GOFR does pattern matching – either using wild cards (the same thing that Microsoft Office uses) or Regular Expressions.
Wild Cards are simpler but less powerful, we recommend using Regular Expressions. Internally, GOFR converts WildCards to Regular Expressions.
To use regular expressions in GOFR, in the multiple parameter entry page click the Option menu (OPT) next to the text to find or text to replace text box and select “Regular Expressions”. The color of the text input box will change to green.
You can use Regular Expressions to transform the result based on the search pattern. For example, you can transform a pattern of <First Name> <Last Name> to <Last Name> <First Name>, fix number formatting, etc. Definitely take a look at the link below.
We have also put together a help file on transformation here: "Transform Text With Wild Cards and Regular Expressions"
There are many resources online that you can use to understand regular expressions. GOFR uses the .NET regular expressions. We like Microsoft’s page itself, it is very comprehensive:
https://learn.microsoft.com/dotnet/standard/base-types/regular-expression-language-quick-reference
Here is a table of basics to get you started:
| Metacharacter | Behavior | Example |
|---|---|---|
* |
Matches the preceding character or subexpression zero or more times. Equivalent to {0,}. |
zo* matches "z" and "zoo". |
+ |
Matches the preceding character or subexpression one or more times. Equivalent to {1,}. |
zo+ matches "zo" and "zoo", but not "z". |
? |
Matches the preceding character or subexpression zero or one time. Equivalent to {0,1}.When ? immediately follows any other quantifier (*, +, ?, {n}, {n,}, or {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible. The default greedy pattern matches as much of the searched string as possible. |
zo? matches "z" and "zo", but not "zoo".o+? matches a single "o" in "oooo", and o+ matches all "o"s.do(es)? matches the "do" in "do" or "does". |
^ |
Matches the position at the start of the searched string. If the multiline option (?m) is placed at the start of the pattern, ^ also matches the position following \n.When used as the first character in a bracket expression, ^ negates the character set. |
^\d{3} matches 3 numeric digits at the start of the searched string.[^abc] matches any character except a, b, and c. |
$ |
Matches the position at the end of the searched string. If the multiline option (?m) is placed at the start of the pattern, $ also matches the position before \n. |
\d{3}$ matches 3 numeric digits at the end of the searched string. |
. |
Matches any single character except the newline character \n. To match any character including the \n, use a pattern like [\s\S]. |
a.c matches "abc", "a1c", and "a-c". |
[] |
Marks the start and end of a bracket expression. | [1-4] matches "1", "2", "3", or "4". [^aAeEiIoOuU] matches any non-vowel character. |
{} |
Marks the start and end of a quantifier expression. | a{2,3} matches "aa" and "aaa". |
() |
Marks the start and end of a subexpression. Subexpressions can be saved for later use. | A(\d) matches "A0" to "A9". The digit is saved for later use ($1 in the text to replace). |
| |
Indicates a choice between two or more items. | z|food matches "z" or "food". (z|f)ood matches "zood" or "food". |
\ |
Marks the next character as a special character, a literal, a backreference, or an octal escape. | \n matches a newline character. \( matches "(". \\ matches "\". |
Paragraphs and line breaks
In Word, PowerPoint and Visio documents, GOFR sees the end of each paragraph, and each line break inside a paragraph, as \n. The paragraphs of one block of text (the body, one table cell, one text box, one footnote...) are searched together, so a pattern can reach from one paragraph into the next. GOFR makes sure it only does that when you ask for it:
- A space,
.,\s,\W,\Dand character classes such as[^,]or[ \t]never match the end of a paragraph. For example\s+with Text To Replace(one space) tidies up spaces and tabs inside each paragraph but never joins two paragraphs, andDear .*,stops at the end of the paragraph if there is no comma in it. - To reach across on purpose, write
\n, put\nin a character class ([ \n]), use[\s\S](any character, including the end of a paragraph), or start the pattern with(?s)so that.matches it as well.{{/p}}in Text To Find means the same as\n. - When the text that is replaced includes the end of a paragraph, the two paragraphs are joined into one. The joined paragraph keeps the formatting of the first one (style, list numbering, alignment, spacing).
- A
\n(or{{/n}}) in Text To Replace inserts a line break, not a new paragraph. To start a new paragraph, use{{/p}}(Word only). ^and$mean the start and the end of the whole block of text, not of each paragraph. Start the pattern with(?m)to make them match at every paragraph:(?m)^Note:finds "Note:" at the start of any paragraph.\tmatches every tab in the document, including the tabs Word uses to line text up at tab stops (tables of contents, forms, signature lines). The old engine matched only tab characters pasted into the text, so a pair list that uses\tcan now change many more places - try it on a copy first.
Useful patterns:
| Text To Find | Text To Replace | What it does |
|---|---|---|
(\w)-\n(\w) |
$1$2 |
Rejoins a word that was hyphenated across two lines ("hyphen-" / "ated" becomes "hyphenated"). |
([a-z,;])\n([a-z]) |
$1 $2 |
Rejoins a sentence that was broken in the middle - common in documents converted from PDF. |
\b(\w+)\n\1\b |
$1 |
Removes a word repeated at the end of one paragraph and the start of the next. |
\n(?=\n) |
(empty) | Removes empty paragraphs. |
[ ]+(?=\n|$) |
(empty) | Removes spaces at the end of paragraphs. (Write the space as [ ]: spaces at the very start or end of a pattern are trimmed off.) |
\b(\w+) \1\b |
$1 |
Removes a doubled word ("the the") inside a paragraph - never across two. |
In Excel, a line break inside a cell is ordinary text: \s and [^,] match it there, and a pattern never reaches from one cell into another.