What would be the regex for replaceAll()
that swaps adjacent chars?
For example, to turn this:
abcdefg
into this:
badcfeg
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 ...
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:
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.
"abcdefg".replaceAll("(.)(.)", "$2$1")
This will return badcfeg
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With