Using Regular Expression in Clean Text for Mac and Clean Text for iOS greatly increases your power and accuracy when doing text processing work. And if you are using the Mac version, you can also create a combination of multiple actions and replacements using the Tasks feature.


The following are some examples that can serve as a basis to get excited about the subject. The advice is to immediately make any real test with Clean Text.





Regular Expression Metacharacters

You can find out how Clean Text uses Regular Expression Metacharacters (the character sequences used to match characters within a string) by visiting this page of Apple's documentation (see Table 1).





Example 1 - Search for all the words starting with "s"


Regular Expression field:

s\w+


Explanation:

"s" is a character

"\w" represents any character included in a word

"+" means "another character like the previous, or more"

Note: you can find more of these expressions on the "Commonly Used Expressions" pop-up menu button




Example 2 - Replace all occurrences of "steve" with "Steve" (capitalized)


Regular Expression field:

s(teve)


Explanation:

"s" is the first character of the word steve

"( )" represents a group of characters, in this expression we have only one group

"teve" is a part of the word steve


Substitution Field:

S$1


Explanation:

"S" is the character s capitalized

"$1" represents the group number 1

So here we ask to replace "s" + "teve" with "S" + the group 1 of the expression field




Example 3 - Exchange the position of two words (e.g. from "Jobs Steve" to "Steve Jobs")


Regular Expression field:

(jobs) (steve)


Explanation:

"( )" represents a group of characters, in this expression we have two groups separated by a space

"jobs" is the first of the two words

" " is the space between the two words

"steve" is the second of the two words


Substitution Field:

$2 $1


Explanation:

"$2" represents the second group

" " is the space between the two words

"$1" represents the first group

So here we ask to replace "Steve" + " " + "Jobs" with the second group + space + the first group




Example 4 - Add spaces after punctuation marks (e.g. from "Kiwi,apple or banana?Kiwi!" to "Kiwi, apple or banana? Kiwi!")


Regular Expression field:

(\w)([.,;:=!?])(\w)


Explanation:

"( )" represents a group of characters, in this expression we have three groups

"[]" represents a "character class" that means "any of the included characters"

".,;:=!?" are the punctuation characters which we want to consider

"\w" represents any character included in a word


Substitution Field:

$1$2 $3


Explanation:

"$1" represents the first group (in this case the character at the end of the word, near the punctation mark)

"$2" represents the second group (the punctation mark)

" " is the space to add after the punctation mark

"$3" represents the third group (in this case at the beginning of the word, near the punctation mark)

So here we ask to replace any sequence of character,character with a space + comma




Example 5 - Search for words starting with something not followed by something else (e.g. find "hello" and "hello3" but excluding "hello2")


Regular Expression field:

hello(?!2)\w?


Explanation:

"hello" is the starting part of word we are searching for

"(?12)" is a "negative lookahead", words that will continue with this string will be excluded from the search, in this case with "2"

"\w" represents any character included in a word

"?" makes preceding character optional so another "character included in a word" can be present or not




Example 6 - Change patterns including numbers (e.g. change "24 years" into "(24)")


Regular Expression field:

(\d+) years


Explanation:

"( )" represents a group of characters, in this expression we have one group

"\d" represents a digit

"+" means "another character like the previous, or more", a digit in this case

Note: with Regular Expressions there are various ways to get the same result, for example we could also use this expression: "([0-9]+) years"



Substitution Field:

($1)


Explanation:

"( )" are simply the brackets in which we want to put the number

"$1" represents the first group (in this case we have only one group but we still have to specify that it is group 1)




Example 7 - Search for more than one term at the same time (e.g. search for "hello" and "steve")


Regular Expression field:

hello|steve


Explanation:

"|" represents the alternation. So A|B matches either A or B.


Note:

If you want to search for multiple words with the same initial part, you can simply use the question mark after each optional character. For example to search for "steve", "steven" and "stevenson", you can simply use: steven?s?o?n?

Naturally, being each character followed by a question mark optional, a possible "steves" will also be matched. To avoid this, you could group the part "son" with parenthesis, in this way: steven?(son)? Now, only "steve", "steven" and "stevenson" will be matched.





Conclusion


The ones we saw were just examples, Regex is a very flexible system, these results can be achieved in many different ways. These are just examples that you can customize as you like.


Have you discovered that you love Regular Expressions (Regex for friends)? There are lot of very fine sites dedicated to learn Regex with tutorials and examples, for example regular-expressions.info. Become a Regex expert, your work on the text will become very fast!





Related solutions