Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to step back to see the big picture and propose this solution to your problem instead:</p> <pre><code>System.out.println( "012345".replaceAll("(.)(.)", "$2$1") ); // "103254" </code></pre> <p>That is, given a <code>String s</code>, <code>s.replaceAll("(.)(.)", "$2$1"))</code> returns a new string where every adjacent pairs of characters in <code>s</code> are swapped. If <code>s</code> has odd length, then the last character remains unswapped. If the string can contain newline characters, then use <code>(?s)</code> embedded <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#DOTALL" rel="nofollow noreferrer"><code>Pattern.DOTALL</code></a>.</p> <p>Essentially the pattern is <code>..</code> (i.e. "any two characters"), but each <code>.</code> is surrounded by brackets to create capturing groups so the captured match can be used in the replacement.</p> <pre><code> MATCH: (.)(.) 1 2 REPLACE WITH: $2$1 </code></pre> <p>In Java regex, e.g. <code>$1</code> in the replacement string refers to what group 1 captured in the match.</p> <h3>References</h3> <ul> <li><a href="http://www.regular-expressions.info/dot.html" rel="nofollow noreferrer">regular-expressions.info/Dot Matches (Almost) Any Character</a> and <a href="http://www.regular-expressions.info/brackets.html" rel="nofollow noreferrer">Brackets for Grouping</a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html#replaceAll%28java.lang.String%29" rel="nofollow noreferrer"><code>java.util.regex.Matcher.replaceAll</code></a></li> </ul> <hr> <h3>Variations</h3> <p>These are provided for more instructional values:</p> <pre><code> System.out.println( "abcdefg".replaceAll("(.)(.)", "$1[$2]") ); // "a[b]c[d]e[f]g" System.out.println( "&gt; Regular expressions: now you have two problems!" .replaceAll("(.)(.)", "$2$1") ); // " &gt;eRugal rxerpseisno:sn woy uoh va ewt orpboelsm!" System.out.println( "&gt; Regular expressions: now you have two problems!" .replaceAll("(\\w)(\\w)", "$2$1") ); // "&gt; eRugalr xerpseisnos: onw oyu ahev wto rpboelsm!" System.out.println( "Wow! Really?? That's awesome!!!" .replaceAll("(.)([!?]+)", "$1$1$1$2$2") ); // "Wowww!! Reallyyy???? That's awesomeee!!!!!!" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload