0

"01:23:45 Computer science is awesome! 10:11:59 I like ICE CREAM." is what this regex is supposed to match.

This is the regex I'm using :

"(\d\d:\d\d:\d\d)\s(.+)\s" Though it's not working...could someone please help me.

Let me specify what I needed done

import java.util.regex.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Friendly {
    public static void main(String[] arguments) throws IOException{
        String input = new BufferedReader(new InputStreamReader(System.in)).readLine();
        Pattern regex = Pattern.compile("(\\d\\d:\\d\\d:\\d\\d)\\s(.+)(?=\\s|$)");
        Matcher match;
        boolean first = true;
        while ((match = regex.matcher(input)).find()){
            if (!first)
                System.out.println();
            else first = !first;
            System.out.print(match.group(1) + " ");
            String[] timestamp = match.group(1).split(":");
            int hour = Integer.parseInt(timestamp[0]) * 9;
            int minutes = Integer.parseInt(timestamp[1]) * 3 + Integer.parseInt(timestamp[2]);
            boolean isOdd = false;
            String[] letters = match.group(2).split("");
            for (int index = 0; index < letters.length; index++){
                String letter = letters[index];
                if (letter != " " && letter.length() > 0){
                    System.out.printf("0x%02X" + (index == letters.length - 1 ? "" : " "), letter.charAt(0)^(isOdd ? minutes : hour) & 0xFF);
                    isOdd = !isOdd;
                }
            }
            input = input.substring(input.indexOf(match.group(0)) + 1, input.length());
        }
    }
}

In this code I'm trying to parse "01:23:45 Computer science is awesome!" then get "10:11:59 I like ICE CREAM."

Sorry if it wasn't clear enough.

3
  • 1
    What does not working mean? Commented Nov 12, 2014 at 4:47
  • I revised my post so it is more clear. Commented Nov 12, 2014 at 4:56
  • if (letter != " " ... do not compare strings like this. You only compare if it is the same string object, which it is probably not. Always. Commented Nov 12, 2014 at 5:18

2 Answers 2

1
(\d\d:\d\d:\d\d(?:(?!\d\d:\d\d:\d\d).)+)

Try this.See demo.

http://regex101.com/r/tF5fT5/42

Sign up to request clarification or add additional context in comments.

6 Comments

Though when I use that it captures 01:23:45 Computer science is awesome! 10:11:59 I like ICE CREAM. I need it to capture "01:23:45 Computer science is awesome!" Sorry I wasn't specific, cause in my code I parse out the string and reapply the regex.
@user2481296 When it errors, does it just say "I am erroring", or does it provide more information that may just be useful to us trying to help you?
It's not because of that here are the logs : 01:23:45 Computer science is awesome! 10:11:59 I like ICE CREAM. 01:23:45 Computer science is awesome! Exception in thread "main" java.lang.NumberFormatException: For input string: "45 Computer science is awesome! " at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at Friendly.main(Friendly.java:19)
@user2481296 the regex is working fine as you can see from the exception.Its the code after that which is causing problems
@vks It's not capturing correctly, please let me specify again that I need it to capture the timestamp and the message with in the regex.
|
0

You forget to take care of ending text.

Try this:

\d\d:\d\d:\d\d\s.+\d\d:\d\d:\d\d\s.+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.