Difference between revisions of "Useful regular expressions (Regex)"
(Reorganized page and added major section regarding numbers in a specific format) |
m |
||
Line 1: | Line 1: | ||
− | A '''regular expression (regex or regexp)''' is a sequence of characters that define a search pattern.<ref>Check out [https://en.wikipedia.org/wiki/Regular_expression#Basic_concepts this article] for a more detailed explanation of the history of regular expressions and how they work.</ref> They can be very helpful in the Editor View for both the filter bar (used to show/hide specific segments) and the Find/Replace dialog. Below are a few examples of useful regexes. In order to use them in [[Wordfast Pro]], make sure any available '''regex''' option is enabled accordingly. | + | A '''regular expression (regex''' or '''regexp)''' is a sequence of characters that define a search pattern.<ref>Check out [https://en.wikipedia.org/wiki/Regular_expression#Basic_concepts this article] for a more detailed explanation of the history of regular expressions and how they work.</ref> They can be very helpful in the Editor View for both the filter bar (used to show/hide specific segments) and the Find/Replace dialog. Below are a few examples of useful regexes. In order to use them in [[Wordfast Pro]], make sure any available '''regex''' option is enabled accordingly. |
==Filter Bar: Showing/Hiding Certain Segments== | ==Filter Bar: Showing/Hiding Certain Segments== |
Revision as of 17:55, 2 June 2021
A regular expression (regex or regexp) is a sequence of characters that define a search pattern.[1] They can be very helpful in the Editor View for both the filter bar (used to show/hide specific segments) and the Find/Replace dialog. Below are a few examples of useful regexes. In order to use them in Wordfast Pro, make sure any available regex option is enabled accordingly.
Contents
Filter Bar: Showing/Hiding Certain Segments
Hide number-only segments
Use the following regex in the segment filtering bar:
^(([0-9][^\n]*[^0-9])|([^0-9][^\n]*[0-9])|([^0-9]?[^\n]*[^0-9]))$
This regex will also hide numbers with punctuation (decimals, etc.)
Show only number-only segments
Use the following regex in the segment filtering bar:
^(?:(?:-|–|(?:(?:\$|€|£)(?:\h)?))?(?:\d{1,3})(?:\h|,|\.|(?:(?:\h)?(?:%|\$|€|£)))?)+$
If you have numbers like 8,675,309.00 that need to be replaced with 8.675.309,00, you can copy all sources to target with the filter applied, then apply a 3-step find and replace:
- Find . and replace with DUMMY
- Find , and replace with .
- Find DUMMY and replace with ,
Show only thousands, ten thousands or hundred thousands surrounded by parentheses
If you negative monetary figures in a financial report, they are generally surrounded by parentheses like this (1,234) or (10,123) or (100,123).
Use the following regex in the segment filtering bar to filter out these segments when the divider is a non-breaking space:
\(\d{1,3}∘\d{3}\)
And this one if the divider is a comma:
\(\d{1,3},\d{3}\)
With the filter in place, copy all sources, then find ∘ and replace with , or vice versa. (∘ represents a non-breaking space).
Show only segments containing numbers in a specific format
Numbers that need to be converted to a different format can be found within any segment containing other text. Depending on the source number format you need to convert, use the corresponding regex from option A, B, or C in the table below to show only those segments for later conversion.
Option | Thousands | Decimal | Example | Regex |
---|---|---|---|---|
A | Space* | Comma | 1 000,00 | (?:\d{1,3})(?:(?:(?:(?<!,\d{3})( |\u00A0|\u2009|\u202F)(?=\d{3}[^\d])))|(?:(?<!\.\d{3}),(?=\d(?!\d+(,\d|\.\d))))) |
B | Dot | Comma | 1.000,00 | (?:\d{1,3})(?:(?:(?<!,\d{3})\.(?=\d{3}[^\d]))|(?:(?<!\d( |\u00A0|\u2009|\u202F)\d{3}),(?! |\d{3}(,[^ ]|\.[^ ])))) |
C | Comma | Dot | 1,000.00 | (?:\d{1,3})(?:(?:(?<!([\d]( |\u00A0|\u2009|\u202F)|(\.))\d{3}),(?=\d{3}[^\d]))|(?:(?<!\d( |\u00A0|\u2009|\u202F)\d{3})\.(?!$| |\d{3}(,[^ ]|\.[^ ])))) |
* Includes no-break space (U+00A0), thin space (U+2009), and narrow no-break space (U+202F)
Filter Bar: Combine with Find/Replace
Replace numbers in a specific format
If you want to convert numbers between the formats in the table above, locate which options you want to convert between in the table below and follow the respective steps.
(A) 1 000,00 to |
(B) 1.000,00 to |
(C) 1,000.00 to |
(C) 1,000.00 to |
---|---|---|---|
|
|
|
|
Find/Replace
Find thousands separated by a non-breaking space and replace NBSP with a comma
Find:
(\d{1,3})∘(\d{3})
Replace with:
$1,$2
Make sure to search target segments only, test on a few segments before replacing all, and run this operation twice to account for numbers in the millions.
Invert currency symbols
Say you have a lot of monetary values like 103,50€ in your document and you want to globally find/replace with €103.50, how would you do this?
Open the Find/Replace function and be sure to tick the Use Regex box.
Type the following regex in the Find what field:
(^[^,]+?)(,)([^€]+?)(€)
Type the following regex in the Replace with field:
\€$1\.$3
NOTE: This only works for values up to 999. Values in the thousands will need another regex operation to replace comma/space/decimal with comma/space/decimal.
References
- ↑ Check out this article for a more detailed explanation of the history of regular expressions and how they work.