1

I am trying to use matcher as an expression to find time stamps in my list of strings. Ex (" [00:00:00.000] ") There are white spaces before and after the time stamp

I checked my regex online and it say it is correct but will not work with my java. It just returns false.

String word = " [00:00:00.000] ";
Pattern pattern = Pattern.compile("^\\s[[0-9:.]*]\\s");
Matcher matcher = pattern.matcher(word);
    if(matcher.matches()){
        //Do Stuff
    else
        //Do other Stuff
2
  • OK.. please be clear.. Do you want to extract the timestamp or just check if your string contains a timestamp? Commented Aug 11, 2015 at 19:22
  • you simply need to escape the first [ like \[ and the last ] like \]. The reason is that the angle brackets are a special character in regular expressions. So the regex checker is wrong. So, I think this would also have caused issues in other programming languages. (Note, in java you will need to escape twice, (\\[ and \\]) Commented Aug 11, 2015 at 19:44

3 Answers 3

2
\\s*\\[[0-9:.]*\\]\\s*

Use this.You dont need ^.escape [].See demo.

https://regex101.com/r/eX9gK2/11

If you want timestamps use

\\s*\\[([0-9:.]*)\\]\\s*

and capture the group 1

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

2 Comments

The OP says to find timestamps. This matches timestamps :)
Well in that case \\[\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\] would have been the right answer
1

Try with:

^\\s\\[[0-9:.]*]\\s

You regex doesn't work because you did't escape first [ character, so it is treated as another character class. You can get timestamp by closing it into group: ([0-9:.]*), but also, if your timestamp always look like this, you can get separate time values with:

^\\s\\[(\\d+):(\\d+):(\\d+)\\.(\\d+)*]\\s

it will give you:

  • hrs - group(1),
  • min - group(2),
  • sec - group(3),
  • msec - group(4),

test it in Java:

public static void main(String args[]){
    String word = " [00:00:00.000] ";
    Pattern pattern = Pattern.compile("^\\s\\[(\\d+):(\\d+):(\\d+)\\.(\\d+)*]\\s");
    Matcher matcher = pattern.matcher(word);
    matcher.find();
    System.out.println(matcher.group(1) + ":" + matcher.group(2) + ":"  + matcher.group(3) + "." + matcher.group(4));
}

3 Comments

The second ] also needs escaping.
@MattBall in Java it is not necessary, it will work, I tested it
@EsotericRider which one?
0
package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class RegexTest {

    @Test
    public void assert_pattern_found() {
        final String word = " [00:00:00.000] ";
        final Pattern pattern = Pattern.compile("^\\s\\[[0-9:.]*\\]\\s");
        final Matcher matcher = pattern.matcher(word);

        assertTrue(matcher.matches());
    }

}

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.