Solved

JAVA Regex Pattern for XML Gregorian Calender

Posted on 2014-04-17
5
406 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
solution works flawless. Thanks to Kaufmed.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
allStar challenge 1 41
word0 challenge 3 56
PDFBox - convert image to PDF, PDF resolution 3 61
eclipse formatting 6 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now