4

I need to simply insert character "/" in the 3rd position of string.

ie "EURUSD" becomes "EUR/USD".

I'm using some program GUI to do this, it accepts a Regex Pattern and a Replacement.

I know this must be super simple but I can't seem to find a simple answer.

3
  • 1
    Can you replace ^(.{3}) with \1/? Commented Dec 27, 2015 at 1:51
  • that gives me "1/USD" Commented Dec 27, 2015 at 1:53
  • 1
    Sorry, $1/, rather? Commented Dec 27, 2015 at 1:55

2 Answers 2

3

Replace ^.{3} (3 [{3}] characters [.] at the start of the string [^]) with $&/ (the match $&, followed by a /).

Sign up to request clarification or add additional context in comments.

Comments

1

Use a look behind:

str = str.replaceAll("(?<=^...)", "/");

Using a look behind, which doesn't consume any input, means the replacement term is simply your new character (no need for back references).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.