Difference between revisions of "Useful regular expressions (Regex)"

From Wordfast Wiki
Jump to: navigation, search
(Show only number-only segments)
Line 1: Line 1:
A regular expression (regex or regexp) is a sequence of characters that define a search pattern. They can be very helpful for filtering out segments in the TXLF Editor or for find/replace operations. 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.
+
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 for filtering out segments in the TXLF Editor or for find/replace operations. Below are a few examples of useful regex. When you use them in [[Wordfast Pro]], make sure to tick the '''regex''' box accordingly.
 
 
Below are a few examples of useful regex.  
 
 
 
'''ATTENTION''': Make sure to tick the '''regex''' box accordingly.
 
 
 
 
 
__TOC__
 
  
 
==Hide all number-only segments==
 
==Hide all number-only segments==
Line 12: Line 5:
 
Use the following regex in the segment filtering bar:
 
Use the following regex in the segment filtering bar:
  
^(([0-9][^\n]*[^0-9])|([^0-9][^\n]*[0-9])|([^0-9]?[^\n]*[^0-9]))$
+
^(([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.)
 
This regex will also hide numbers with punctuation (decimals, etc.)
 
  
 
==Show only number-only segments==
 
==Show only number-only segments==
Line 21: Line 13:
 
Use the following regex in the segment filtering bar:
 
Use the following regex in the segment filtering bar:
  
^(?:(?:-|–|(?:(?:\$|€|£)(?:\h)?))?(?:\d{1,3})(?:\h|,|\.|(?:(?:\h)?(?:%|\$|€|£)))?)+$
+
^(?:(?:-|–|(?:(?:\$|€|£)(?:\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:
 
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:
Line 36: Line 28:
 
Type the following regex in the ''Find what'' field:
 
Type the following regex in the ''Find what'' field:
  
(^[^,]+?)(,)([^€]+?)(€)
+
(^[^,]+?)(,)([^€]+?)(€)
  
 
Type the following regex in the ''Replace with'' field:
 
Type the following regex in the ''Replace with'' field:
  
\€$1\.$3
+
\€$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.
 
 
 
  
----
+
'''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.
Version: Wordfast Pro 5.8<br>
 
Operating System: macOS
 
  
--[[User:John|John]], 17 May 2019
+
==References==
  
[[Category:Wordfast Pro 5]]
+
[[Category:Wordfast Pro]]

Revision as of 21:58, 16 April 2021

A regular expression (regex or regexp) is a sequence of characters that define a search pattern.[1] They can be very helpful for filtering out segments in the TXLF Editor or for find/replace operations. Below are a few examples of useful regex. When you use them in Wordfast Pro, make sure to tick the regex box accordingly.

Hide all 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:

  1. Find . and replace with DUMMY
  2. Find , and replace with .
  3. Find DUMMY and replace with ,

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

  1. Check out this article for a more detailed explanation of the history of regular expressions and how they work.