Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for simple shuffle using String.replaceAll

Tags:

java

regex

What would be the regex for replaceAll() that swaps adjacent chars?

For example, to turn this:

abcdefg

into this:

badcfeg
like image 461
Bohemian Avatar asked Dec 28 '12 08:12

Bohemian


People also ask

How to use multiple regex patterns with replaceAll (Java String class)?

How to use multiple regex patterns with replaceAll (Java String class) 1 A simple string#N#First, I create a simple String:#N#scala> val s = "My dog ate all of the cheese; why, I don't know." s:... 2 Replace multiple patterns in that string#N#In my case I want to remove all trailing periods, commas, semi-colons, and... 3 More explanation More ...

What is the purpose of this regex search tool?

It is intended to find the following regex patterns in the given string: The combination of the brackets and pipe characters is what makes this work. It’s often hard to read regex patterns, so it may help to look at that multiple search pattern regex more generally, like this:

What is a regular expression?

A regular expression that matches a slash at the end of a string. A regular expression that matches the first word after a specific word in a sentence. A regular expression that determines if a given string has all unique (none repeating) characters. A regular expression that matches multiple Email addresses separated by semicolons in a string.


1 Answers

"abcdefg".replaceAll("(.)(.)", "$2$1")

This will return badcfeg.

like image 130
Patrickdev Avatar answered Oct 26 '22 23:10

Patrickdev