1

gcc 4.6.2 c89

Is there any standard regex expression that can be used to extract the IP address from a string.

I am thinking about using sscanf with a regex expression to get the IP from this sample string below.

This is the sample string:

v=0 o=sip_user IN 10230 22472 IP4 NET.CAT.NTBC s=SIP_CALL c=IN IP4 10.10.10.44 m=audio 49152 RTP/AVP 0 a=rtpmap:0 PCMU/8000

So the regex will be in between the quotes:

sscanf(ip_string, "%s", &ip_address);

Many thanks for any suggestions,

1
  • 1
    sscanf() doesn't take a regex. Commented Dec 9, 2011 at 0:30

3 Answers 3

4
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
Sign up to request clarification or add additional context in comments.

1 Comment

Hello. Thanks I did see that regular regex expression. But my problem is how can I get that to work in c? What standard c function do I insert it? Many thanks.
1

Check your OS for regex.h and use the defined regular expression library. e.g. linux example

Comments

1

Here is a short concise and easy-to-read regex with 34 characters:

^(25[0-5]|2[0-4]\d|[01]?\d\d?\.?){4}$ 

This is matching only numbers between 0 to 255.

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.