Results 1 to 8 of 8
  1. #1
    Castle is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default regex to just print one line

    httpHead request returns a few lines of data which look like this:
    Name: Date Value: ....
    Name: Server Value: ...
    Name: Last-Modified Value: ...

    (With the relevant values)

    I want to use regex to just print one part of one of the lines, for example, if I wanted to print the Date line, I would just want the value of it, so:

    Tue, 27 Nov 2012 22:27:17 GMT

    Would be my only output.

    I'm not sure how to implement this so that it only prints that and not everything?

    Any ideas?

  2. #2
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: regex to just print one line

    Have you tried if statements?

  3. #3
    Castle is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: regex to just print one line

    Quote Originally Posted by Dr_DerpyHooves View Post
    Have you tried if statements?
    Not sure how if statements would work for that?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: regex to just print one line

    Isn't there any character that separating each line of this data? If you have this character then you can simply split the lines into an array. And then you can access the data you want from this array.

  5. #5
    Castle is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: regex to just print one line

    Quote Originally Posted by wsaryada View Post
    Isn't there any character that separating each line of this data? If you have this character then you can simply split the lines into an array. And then you can access the data you want from this array.
    There is just whitespace separating them. I just want to print one of the values and I don't know how to distinguish between them because they all have the same label ie Name: and Value:

    It's just printing the Date or the Last-Modified Date, but that's it, just the date nothing else.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: regex to just print one line

    Can you post your data sample. I want to see the actual data how it looks like.

  7. #7
    Castle is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: regex to just print one line

    Java Code:
    public void print(ArrayList<String> urls) {
    		for(String item: urls){
    			System.out.println(item);
    			String lmod = client.httpHead(item);
    		}			
    			
    	}
    Sample Output:

    www.google.co.uk/services/hp/index.html
    Name: Date Value: Wed, 28 Nov 2012 01:15:33 GMT
    Name: Server Value: Apache/2.2.22 (Linux/SUSE)
    Name: Last-Modified Value: Tue, 30 Oct 2012 13:09:57 GMT
    Name: ETag Value: "50d-1d4-4cd4680b39cf0"
    Name: Accept-Ranges Value: bytes
    Name: Content-Length Value: 468
    Name: Keep-Alive Value: timeout=15, max=100
    Name: Connection Value: Keep-Alive
    Name: Content-Type Value: text/html

    I just want to print:

    Tue, 30 Oct 2012 13:09:57 GMT

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: regex to just print one line

    Let's get back to the first step. This data is an HTTP response headers right? What library are you currently using in you program? Are you working for example using the Apache HttpClient library?

    Can you post some more code? Like what is in the client.httpHead() method.

Similar Threads

  1. Where does he print this certain line?
    By Lund01 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2010, 02:23 PM
  2. print every other line
    By welikedogs in forum New To Java
    Replies: 23
    Last Post: 11-16-2010, 05:40 PM
  3. Regex Pattern/Matcher - Print only one occurrence!
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-06-2009, 05:05 PM
  4. Can't make regex ignore line terminator
    By mobilityguy in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 04:27 PM
  5. print line
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 01-26-2008, 02:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •