1

I have a string 'ABC.1.2.3' I wish to replace the middle number with 1.

Input 'ABC.1.2.3'
Output 'ABC.1.1.3'

Input 'XYZ.2.2.1'
Output 'XYZ.2.1.1'

The is, replace the number after second occurrence of '.' with 1.

I know my pattern is wrong, the sql that I have at the moment is :

select REGEXP_REPLACE ('ABC.1.2.8', '(\.)', '.1.') from dual;

2 Answers 2

1

You can use capturing groups to refer to surrounding numbers in replacement string later:

select REGEXP_REPLACE ('ABC.1.2.8', '([0-9])\.[0-9]+\.([0-9])', '\1.1.\2') from dual;
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this worked, can you please explain what the pattern and replace strings are, I understand that pattern is "number.number.number", but not able to figure what the replace string mean
first and third number are inside a capturing group. Capturing groups let us to refer to their stored value later on replacement string. \1 is a back-reference to capturing group one and \2 is a back-reference to capturing group two. We remove whole thing then rebuild with ours. @vick_4444
Understood. Thank you very much.
1

You could use

^([^.]*\.[^.]*\.)\d+(.*)

See a demo on regex101.com.


This is:

^                # start of the string
([^.]*\.[^.]*\.) # capture anything including the second dot
\d+              # 1+ digits
(.*)             # the rest of the string up to the end

This is replaced by

$11$2

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.