0

I'm trying to parse log using regex. Unfortunately I' stacked on following string. Trying to find line beginning with time

for example:

String p="11:33:00.0   2000           0.0     ....... #           0.0     ....... #           0.0     ....... #           0.0     ...";

I have following code:

public class Test {


public static void main(String[] args) {
  String p="11:33:00.0   2000           0.0     ....... #           0.0     ....... #           0.0     ....... #           0.0     ...";
  Pattern pat = Pattern.compile("^\\d\\d\\:\\d\\d*");
  Matcher m = pat.matcher(p);
  if (m.find()) {
    System.out.println(m.start());
    System.out.println(p.substring(m.start()));
  }

}
}

this code prints nothing even if I tried just '^\d\d'.

if I'm correct '^' stands for line beginning '\d' for any digit

I also tried to replace '^' with '\A' If i change pattern to

pat = Pattern.compile("\\d\\d");

it returns position at 6. Can somebody tell me why the first code is not working?:)

THX

2 Answers 2

1

You need to print the group index 0 inside the if block, so that it would print the matched characters.

String p="11:33:00.0   2000           0.0     ....... #           0.0     ....... #           0.0     ....... #           0.0     ...";
Pattern pat = Pattern.compile("^\\d\\d\\:\\d\\d*");
Matcher m = pat.matcher(p);
if (m.find()) {
    System.out.println(m.group(0));
}

Output:

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

3 Comments

@sala84 what's your expected output?
syso is just check, my problem is I can't get into if, with current code and I don't know why I have no output. You changed nothing and you still have print:)
the above code works for me. i just added this System.out.println(m.group(0)); line only.
0

On my computer your example Test.main worked fine, maybe it's dependant on JVM implementation. I think if you open then you also need to close regex with question mark:

"^\\d\\d?"

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.