Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JAVA Regex Pattern for XML Gregorian Calender

Posted on 2014-04-17
5
Medium Priority
?
429 Views
Last Modified: 2014-04-17
I have to read file that has text as "#EXT-X-PROGRAM-DATE-TIME:2014-04-17T07:18:02.350+00:00" for every 10 seconds and ill be having such files about 400.

I need to fetch value 2014-04-17T07:18:02.350+00:00

I did a sample program that picks the above value with  substring.

 String aacKey = strLine.substring(strLine.indexOf(#EXT-X-PROGRAM-DATE-TIME:) + 5, strLine.length() - 1);

However, this looks like a costly approach where i see char[] is taking too much of heap space where allocated space isn't enough for the operations.

What is the best approach to address this. Also, what is the regular expression that i can use to fetch using Pattern, Matcher instead of substring().

Appreciate your response.
Thanks :)
0
Comment
Question by:VAnvesh
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40007828
what is the regular expression that i can use
String pattern = "\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+\\d{2}:\\d{2}";

Open in new window

0
 

Author Comment

by:VAnvesh
ID: 40007876
Hi Kaufmed,

I understand the expression and i believe it is correct. However, when i used for the below program
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringRegex {

    public static void main(String [] a){
        String value = "#EXT-X-PROGRAM-DATE-TIME:2014-04-17T07:18:02.350+00:00";
        Pattern p = Pattern.compile("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+\\d{2}:\\d{2}");
        Matcher m = p.matcher(value);
        if(m.find())
        System.out.println(m.group(1));
    }
}

Open in new window


I am unable to get the desired result. Can you let me know where i go wrong.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40007893
Try group 0 instead--I didn't include any capture groups in the pattern. Or, you can surround the pattern with parens (see below) and you can stick with group 1.

Pattern p = Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+\\d{2}:\\d{2})");

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40007908
P.S.

I forgot to include one character:

Pattern p = Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d+\\+\\d{2}:\\d{2})");

Open in new window


Forgot the +
0
 

Author Closing Comment

by:VAnvesh
ID: 40007970
solution works flawless. Thanks to Kaufmed.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question