top of page



DataWeave programming challenge #5: Reverse a phrase's words, but keep the punctuation


In this post:


This challenge is based on Codeacademy's Reverse Words challenge

Try to solve this challenge on your own to maximize learning. We recommend you refer to the DataWeave documentation only. Try to avoid using Google or asking others so you can learn on your own and become a DataWeave expert!


Consider the following input payload (can be of txt format):

Hello world
May the Fourth be with you
Hello world!
With you, be May the Fourth
With you, be May the Fourth!

Explanation of the problem

Create a DataWeave script that will reverse the order of each one of the words in a phrase to form a new phrase. Each phrase or sentence is separated by a new line. Each word is separated by a space. However, the punctuation signs (! and ,) will have to stay in the same place. Lower/upper cases can be kept as-is.

For example:

  • "Hello world!" becomes "world Hello!"

  • "With you, be May the Fourth!" becomes "Fourth the, May be you With!"

Expected output

In this case, the expected output would be:

  "world Hello",
  "you with be Fourth the May",
  "world Hello!",
  "Fourth the, May be you With",
  "Fourth the, May be you With!"

ℹ️ Note: You can keep the output as a JSON or as a text, whatever your preference is.


If you're stuck with your solution, feel free to check out some of these clues to give you ideas on how to solve it!

Clue #1

Clue #2

Clue #3

Clue #4