Solved

JAVA Regex Pattern for XML Gregorian Calender

Posted on 2014-04-17
5
414 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 133
Java Loop 6 61
hibernate example using maven 12 42
How to execute a Python program and gather return output in Java 2 30
For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

777 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