Question

How to extract the date and ip address of a string in java using regex. Example:...

How to extract the date and ip address of a string in java using regex.

Example:

10.223.157.186 - - [15/Jul/2009:14:58:59 -0700] "GET / HTTP/1.1" 403 202

given an input like the string above, I want to extract the ip address and the date so that the output is:

Ip address: 10.223.157.186

Date: 15/Jul/2009:14:58:59

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

Code :


package ip_date_regex;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IP_DATE_regex {

private static Pattern IP_PTN = Pattern.compile("(d{1,3}).(d{1,3}).(d{1,3}).(d{1,3})");//regular expression for ip address
private static Pattern DATE_PTN= Pattern.compile("[0-9]{1,2}/(?:Jan|Feb|Mar|Apr|May|June?|July?|Aug|Sept?|Oct|Nov|Dec)/dddd:(00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):[0-59]{1,2}");//regular expression for date

public static List<String> FindIpAdress(String Text)//method(function) for match or find ip address
{
Matcher match = IP_PTN.matcher(Text);
List<String> ipadd = new ArrayList<String>();
while(match.find())
{
ipadd.add(match.group());//if find matching,store in ipadd until all matches found
}
return ipadd;
}
  
public static List<String> FindDate(String Text)//method(function) for match or find date
{

Matcher match = DATE_PTN.matcher(Text);
List<String> date = new ArrayList<String>();
while(match.find())
{
date.add(match.group());//if find matching,store in date until all matches found
}
return date;//return matching date
}
public static void main(String[] args)
{
String str = "10.223.157.186 - - [15/Jul/2009:14:58:59 -0700] "GET / HTTP/1.1" 403 202";//string

System.out.println("Ip address:"+FindIpAdress(str));//FindIpAdress method called
System.out.println("Date:"+FindDate(str));//FindDate method called
}
  
}
Output:

Thank you if you have any query regarding above answer please ask me in comment box.

if you like my work appreciate with thumbs up.

Thank You.

Add a comment
Know the answer?
Add Answer to:
How to extract the date and ip address of a string in java using regex. Example:...
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
  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • 166 Chapter 8: TCP/IP Applications Getting Down to Business The way network communication all those ls...

    166 Chapter 8: TCP/IP Applications Getting Down to Business The way network communication all those ls and Os) goes in and out of a machine physically is through the NIC (network interface card). The way network communication goes in and out of a machine logically though, is through a program or service. A service is a program that runs in the background, independent of a logon, that provides functionalities to a system. Windows client machines, for instance, have a Workstation...

  • 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...

  • How do I get my output to print "What class of car do you want" after every loop? Here is my outp...

    How do I get my output to print "What class of car do you want" after every loop? Here is my output, it gets stuck on asking and calculating the cost of the specific class of car i entered previously. clear cic: cars-input ('How many cars do you want to rentln run input ('Would you like to run the program Y or N','S) while run'Y carclass-input ('What class of car do you want B, C, or DIn', 'S while carclassB...

  • 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....

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • Project: Using Java API Classes Page 3 of 5 SpaceTicket.java Requirements: The purpose of this program is accept coded...

    Project: Using Java API Classes Page 3 of 5 SpaceTicket.java Requirements: The purpose of this program is accept coded space ticket information as input that includes the ticket price, category, time, date, and seat, followed by the description of the travel. Note that the eight digits for price have an implied decimal point. The program should then print the ticket information including the actual cost, which is the price with discount applied as appropriate: 25% for a student ticket (s),...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Java Code please read comments and add the code to required lines....LINE 1 to LINE 5...

    Java Code please read comments and add the code to required lines....LINE 1 to LINE 5 ********************************************************************** * Program Summary: This program demonstrates these basic Java concepts: * - Creating an array based on user input * - Accessing and displaying elements of the array * * The program should declare an array to hold 10 integers. * The program should then ask the user for an integer. * The program should populate the array by assigning the user-input integer...

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
ADVERTISEMENT