1

Given the string: "+49 ( 0 ) 162 12345 0162"

And the regex: ^(+\s*4\s*9\s*(\s*0\s*)|+\s*4\s*9|0\s*0\s*4\s*9|0|(\s*0\s*))\s*(15|16|17)

It matches: "+49 ( 0 ) 16"

Now I want to replace everything before 16, so that results in "162 12345 0162".

I have so far:

        Regex regex = new Regex( @"^(\+\s*4\s*9\s*\(\s*0\s*\)|\+\s*4\s*9|0\s*0\s*4\s*9|0|\(\s*0\s*\))\s*(15|16|17)");
        string result = regex.Replace( "+49 ( 0 ) 162 12345 0162", "" );

But that returns "2 12345 0162".

Thank you!

2 Answers 2

1

(15|16|17) part of your pattern - the second capturing group - matches and captures 16. Thus, you need to replace with $2 backreference.

string result = regex.Replace( "+49 ( 0 ) 162 12345 0162", "$2" );

See the regex demo

enter image description here

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

1 Comment

Thank you very much
0

Is (15|16|17) at the end really necessary? Removing it will give you desired result.

1 Comment

Yes it's necessary, because I dont want to match i.e. "+49 ( 0 ) 142 12345 0162"

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.