Reputation: 7053
So I'm trying to verify the time zone section of a dateTime using a regex just for the "GMT" or "UTC" section. A valid String looks like:
"Wed, 8 Feb 2016 11:20:00 [GMT]"
I've tried the following but its not working. I've tried a bunch of different escape sequences but to no avail.
assertTrue(dateTime.getDateTime().matches("Wed, 8 Feb 2016 11:20:00 [\\w{3}]"));
I think the problem I'm having is due to the square brackets that I need being treated as part of the regEx.
Upvotes: 0
Views: 226
Reputation: 37404
You need to escape the []
because []
represents a list which is used to find the match so use it like Wed, 8 Feb 2016 11:20:00 \\[\\w{3}\\]
\\[\\w{3}\\]
\\[
: match [
character
\\w{3}
: match 3 times \\w
,\\w
mean [a-zA-Z0-9_]
\\]
: match ]
character
String date= "Wed, 8 Feb 2016 11:20:00 [GMT]";
System.out.println(date.matches("Wed, 8 Feb 2016 11:20:00 \\[\\w{3}\\]"));
Oracle Regex Character Classes
Upvotes: 3