Position of Apostrophe in Java Regex -
i'm trying see if log pattern layout has timestamp/date field , position in layout when splitted spaces. seems position of apostrophe in regular expression matters.
e.g. when have pattern .*%d[ate]*\\{([\\w\\.'-\\:]+)}.*
matches 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 interchange -
, '
in regular expression, runtime error below.
exception in thread "main" java.lang.exceptionininitializererror caused by: java.util.regex.patternsyntaxexception: illegal character range near index 19 .*%d[ate]*\{([\w\.-'\:]+)}.* ^ @ java.util.regex.pattern.error(pattern.java:1955) @ java.util.regex.pattern.range(pattern.java:2655) @ java.util.regex.pattern.clazz(pattern.java:2562) @ java.util.regex.pattern.sequence(pattern.java:2063) @ java.util.regex.pattern.expr(pattern.java:1996) @ java.util.regex.pattern.group0(pattern.java:2905) @ java.util.regex.pattern.sequence(pattern.java:2051) @ java.util.regex.pattern.expr(pattern.java:1996) @ java.util.regex.pattern.compile(pattern.java:1696) @ java.util.regex.pattern.<init>(pattern.java:1351) @ java.util.regex.pattern.compile(pattern.java:1028)
if explain significance of position of apostrophe, understand concept of regular expressions better.
you seems escape wrong characters inside character class. notice -
special character used create range a-z
.-'
not correct range since index of .
not before '
in unicode table.
to make -
simple literal either escape "\\-"
or place @ start/end of character class ([-...]
[...-]
) can no longer treated range operator.
also don't need escape .
or :
in character class.
Comments
Post a Comment