Removing repeated words from text string
In this recipe, we will use the start and end of word operators along with the subgroup register placeholders in order to write a program that will remove adjacent duplicate words from a text string. For example, from the text 'this this is is a repeated text text 11 11'
, the duplication of words will be removed and the new text 'this is a repeated text 11'
is given as the output.
How to do it...
In order to create a repeated word removal program, proceed as follows:
Declare the
textstream
string. Then assign some text to it that has repeated words in it.A
replace all occurrences
statement is then written with the regular expression(\<\w+\>) \1
. The replacement key is'$1'
.The
if
statement is then used for checking the return code. Forsy-subrc
having the value0
, the messageNumber is Valid
is displayed.
How it works...
The regex used in this recipe is different from that used in the previous one. Since we require searching of duplicate words rather...