Position of Apostrophe in Java Regex

I’m trying to see if a log pattern layout has a timestamp/date field and its position in the layout when splitted by spaces. It seems like the position of apostrophe in the regular expression matters.

E.g. when I have the pattern .*%d[ate]*{([w.'-:]+)}.* it matches a layout of format %X{IP} %X{field1} %X{field2} [%date{yyyy-MM-dd'T'HH:mm:ssZ} guid=%{guid} userId=%{userId} %msg%n. However, when I interchange the - and ' in the regular expression, I get a runtime error as below.

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.util.regex.PatternSyntaxException: Illegal character range near index 19
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.range(Pattern.java:2655)
    at java.util.regex.Pattern.clazz(Pattern.java:2562)
    at java.util.regex.Pattern.sequence(Pattern.java:2063)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.compile(Pattern.java:1696)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)

If you could explain the significance of the position of apostrophe, it would really help understand the concept of Regular expressions better.

Source: regex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.