0

I have a string of text that contains an IP address with hyphens and words. I want to extract the IP address from it using a regular expression that also converts the hyphens to periods.

So to clarify, can the IP address be extracted and the hyphens be replaced with periods purely with a regular expression without using the regex in some code such as Java or Python:

What I have

ip-10-179-50-22.corp.dept.org.uk

What I want

10.179.50.22

I'm thinking that I need to extract 4 groups based on numbers and concatenate them with periods, but I have no idea how to do this or if this is even possible with only regex?

What I've tried

(\d{2,3}-\d{2,3}-\d{2,3}-\d{2,3})

This gives me back 10-179-50-22 but I don't know how to replace on the matching group.

EDIT

I edited the question to clarify that I was trying to find a solution to find and replace with only a regular expression.

In summary, it looks like you can't just use a regex, but the regex needs to used with some code to achieve this

7
  • 2
    What is your code? Commented Apr 4, 2022 at 18:01
  • Not sure I understand your comment @WiktorStribiżew - the section above called 'What I've tried' is my code attempt Commented Apr 4, 2022 at 18:06
  • How to replace is language-specific. Which language are you using? Commented Apr 4, 2022 at 18:06
  • 1
    There is only a regex string. Where are you using it? How? If you type the regex in Notepad++, it won't do anything, you need to put it into a specific field in a specific window. If you write it down on a Web page, but do not use it in any code, you won't get the job done either. Regex is a piece of text. It describes some specific text pattern. It depends on the code that uses the regex what it finally does. Commented Apr 4, 2022 at 18:06
  • I was attempting this on on regex101.com This will end up inside an ansible jinja2 file which I believe is interpreted by Python Commented Apr 4, 2022 at 18:12

3 Answers 3

1

We don't know which language your using but a lot of them have a .replace() with which you could change - for . or event sub string with a bit of adjusting should work for your problem.

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

2 Comments

Thanks for your answer. I was hoping for a pure regex to do this. Is it even possible?
To my knowledge you still need a replace method() but it may vary between the technologies you might be using. Insight on your project might help use find a solution
1

See regex101 example.

Regex:

.*\b(\d{2,3})-(\d{2,3})-(\d{2,3})-(\d{2,3})\b.*

Substitution:

\1.\2.\3.\4

Result:

10.179.50.22

Note that I am just using the regex you provided to match the IP address part, rather than a more accurate regex that would match any valid IP address.

Comments

0

If you are able to use the sed command, this regex would do the job. It uses 4 groups like you suggested.

sed -i 's/^ip-\([0-9]*\)-\([0-9]*\)-\([0-9]*\)-\([0-9]*\).*/\1.\2.\3.\4/g'

It replaces ip-10-179-50-22.corp.dept.org.uk with 10.179.50.22. You can test it on https://sed.js.org.

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.