当前位置: 动力学知识库 > 问答 > 编程问答 >

replace - How can i move words around in a sentence using regex?

问题描述:

So i have the following sentence: "How quickly daft jumping zebras vex."

and i need to come up with a single regex expression that will transform that sentence into this:

"vex zebras jumping daft quickly How."

Can anyone please help me figure this out?

网友答案:

As mentioned in the comments, regex is not meant for this, since it can't be known how many words there will be. But with your limited example, this works:

^(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\s+(\w+)\.$

Replace with

$6 $5 $4 $3 $2 $1.

Try it (press [Java]): http://fiddle.re/67kef

分享给朋友:
您可能感兴趣的文章:
随机阅读: