?
Solved

JAVA Regex Pattern for XML Gregorian Calender

Posted on 2014-04-17
5
Medium Priority
?
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
The task of choosing a web design company to build a website for your business should never be taken in a light manner. Provided the fact that your website will act as a representative to your business and will be responsible for imposing an online …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

718 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