Question

6. (If you cannot get the exact regular expression, put one that is as close as...

6. (If you cannot get the exact regular expression, put one that is as close as you can. Specify what is wrong with it.)An IP address is often written as four decimal numbers ranging from 0 to 255 with dots between them. For example the database server that we use for this class is 163.238.35.165

a. Write a regular expression that matches IP addresses of this type. You can probably find this answer online, but try to do it yourself first.

b. Often the first two numbers correspond to the network (163.238 in this example) and the second two correspond to the host (35.165 in this example). Write the php code that checks a string to see if it is a valid ip address. If it is, the code prints out the network and host. If it is not, the code prints “ILLEGAL IP ADDRESS”

Write the php code that checks a string to see if it is a valid ip address. If it is, the code prints out the network and host. If it is not, the code prints “ILLEGAL IP ADDRESS”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) REGEX FOR IP ADDRESS : Please follow image attached. I have mentioned how each octet is represented by arrows and explanation.

-----------------------------------------------------------------------------------------------

b) PHP CODE TO VALIDATE AN IP ADDRESS

<?php

$ipAddr1 = "163.238.35.165"; // to check for valid ip address
$ipAddr2 = "16.238"; // to check for illegal ip address

/* The filter_var() function filters a variable with the specified filter.
It takes two arguements, first the variable to be filtered and next is
   the FILTER_VALIDATE_IP filter which validates an IP address. */


if (filter_var($ipAddr1, FILTER_VALIDATE_IP))
{
echo("LEGAL IP ADDRESS:: ");
  
/* explode is a funtion to split the a string object into and array of objects
and the splitting is based on a particulat string, like colon, semicolon, dot, /, etc */
  
$ipAddr1Array = explode (".", $ipAddr1);
  
/* after exploding the array looks like this :
ipAddr1Array = { "163", "238", "35", "165"} and we can access by their index starting from 0
length-1.
*/
  
echo " NetworkID : ". $ipAddr1Array[0].".". $ipAddr1Array[1];
echo " and HostID : ". $ipAddr1Array[2].".". $ipAddr1Array[3];
}
else
{
echo("ILLEGAL IP ADDRESS");
}
  
// checking for ipAddr2 which is an illegal address
if (filter_var($ipAddr2, FILTER_VALIDATE_IP))
{
echo("\nLEGAL IP ADDRESS");
}
else
{
echo("\nILLEGAL IP ADDRESS");
}
  
?>

Add a comment
Know the answer?
Add Answer to:
6. (If you cannot get the exact regular expression, put one that is as close as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write. Feel free to create a form/GUI or use console to create a regular expression...

    C++ Write. Feel free to create a form/GUI or use console to create a regular expression to determine if a phone number input is valid. You will need to import a regular expression library. My code below as an example.          string phoneNum =txtName.Text;          string MatchPhonePattern = @"\(?\d{3}\)?[. -]? *\d{3}[. -]? *[. -]?\d{4}";       Regex rx = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);         Match match = rx.Match(phoneNum);         if (match.Success)             lblDisplay.Text = "Valid number";         else             lblDisplay.Text...

  • MUST BE IN C++ The lab is called "7.5.9.1 Exceptions: including information in exceptions" The lab...

    MUST BE IN C++ The lab is called "7.5.9.1 Exceptions: including information in exceptions" The lab is as follows: Objectives Familiarize the student with: using exceptions in real programs; the simplification of exception throwing; throwing exceptions in constructors. Scenario IP header – imagine you have a class or struct (choose one) describing an IP header which holds (apart from other fields) two string fields that contain Source IP Address and Destination IP Address. Check these two fields and throw an...

  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

  • Preliminaries For this lab you will be working with regular expressions in Python. Various functions for...

    Preliminaries For this lab you will be working with regular expressions in Python. Various functions for working with regular expressions are available in the re module. Fortunately, Python makes it pretty easy to check if a string matches a particular pattern. At the top of the file we must import the re module: import re Then we can use the search() function to test whether a string matches a pattern. In the example below, the regular expression has been saved...

  • IN PYTHON. We have seen examples of using the python re library. In this practice, you...

    IN PYTHON. We have seen examples of using the python re library. In this practice, you need to write python code and complete following tasks. (You can decide whether you want to define functions and how to organize your code. It is for your own practice.) 1). Define 10 string variables that follows the requirement given an integer, a float, a double, a float end with letter f (4.321f), Capital Letters( followed by small case letters, followed by digits, Exactly...

  • Project 4-3: In the following steps, you will learn more about the PING (Packet Internet Grouper)...

    Project 4-3: In the following steps, you will learn more about the PING (Packet Internet Grouper) utility, which can be used on a network running IPv4 to verify that TCP/IP is configured correctly and communicating with the network. A ping test is typically the first thing network professionals try when troubleshooting a TCP/IP connection problem. For this project, you can use any type of workstation that's running TCP/IPv4, preferably one connected to the Internet. The output described in the following...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

  • Overview This assignment will give you experience on the use of classes. Understand the Application Every...

    Overview This assignment will give you experience on the use of classes. Understand the Application Every internet user–perhaps better thought of as an Internet connection–has certain data associated with him/her/it. We will oversimplify this by symbolizing such data with only two fields: a name ("Aristotle") and a globally accessible IP address ("139.12.85.191"). We could enhance these by adding other—sometimes optional, sometimes needed—data (such as a MAC address, port, local IP address, gateway, etc), but we keep things simple and use...

  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

  • Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...

    Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need to understand function, pointer and the selection sort algorithm. Write a C program that consists of three files mysort.h, mysort.c and myMain.c. Below is the mysort.h prototype #include <stdlib.h> #include <stdio.h> void generateNums(int *myarr, int len); void sortNums(int *myarr, int len); mysort.h must contain only the function declarations (prototypes) listed above generateNums function should generate len random integers in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT