Why does this Java regex cause "illegal escape character" errors?

I'm working on a solution to a previous question, as best as I can, using regular expressions. My pattern is


According to NetBeans, I have two illegal escape characters. I'm guessing it has to do with the \d and \w, but those are both valid in Java. Perhaps my syntax for a Java regular expression is off...

The entire line of code that is involved is:

userTimestampField = new FormattedTextField( new RegexFormatter( "\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}" ));

-------------Problems Reply------------

Assuming this regex is inside a Java String literal, you need to escape the backslashes for your \d and \w tags:


This gets more, well, bonkers frankly, when you want to match backslashes:

public static void main(String[] args) {
Pattern p = Pattern.compile("\\\\\\\\"); //ERM, YEP: 8 OF THEM
String s = "\\\\";
Matcher m = p.matcher(s);


What about the following: \\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}

Did you tried this?


all you need to do is to put

ex: string ex = 'this is the character: *\\s';

before your invalid character and not 8 \ !!!!!

I think you need to add the two escaped shortcuts into character classes. Try this: "[\d]{4}[\w]{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

--Good Luck.

Did you try "\\d" and "\\w"?

-edit- Lol I posted the right answer and get down voted and then I notice that stackoverflow escapes backslashes so my answer appeared wrong :)

Category:java Views:0 Time:2009-09-04
Tags: regex java

Related post

  • Illegal escape character error in Java regex 2011-11-16

    I've read the manual, and at the end there was an exercise: Use a backreference to write an expression that will match a person's name only if that person's first name and last name are the same. I've written the next program http://pastebin.com/YkuU

  • Java Regex Illegal Escape Character in Character Class 2011-09-30

    I'm trying to determine whether or not a expression passed into my Expressions class has an operator. Either +-*/^ for add, subtract, multiply, divide, and exponent respectively. What is wrong with this code? private static boolean hasOperator(String

  • Illegal Escape Character "\" 2011-06-06

    i want to get the name at the final of a link so i did that if( invName.substring(j,k).equals("\")){ copyf=invName.substring(0,j);} so eclipse said String literal is not properly closed by a double-quote how can i compaire String with this char \ ---

  • Illegal pattern character 'T' when parsing a date string to java.Date 2010-04-08

    I have a date string and I wang to parse it to normal date use the java Date API,the following is my code: public static void main(String[] args) { String date="2010-10-02T12:23:23Z"; String pattern="yyyy-MM-ddThh:mm:ssZ"; SimpleDateFormat sdf=new Si

  • llegal escape character followed by a space 2012-03-02

    I'm writing a bit of code to run a shell script using process that loads and runs a file in terminal. The problem I'm having is getting the filename to be recognised by terminal due to the spaces, for example : "$ ./run_file.sh foo bar.ss" should be

  • Escaping special characters in java regex (not quoting) 2011-08-04

    I'm trying to match user input with wildcards that are simpler than the java regex syntax. Say there's a wildcard A. The user would then enter the input string: this ( is \ a $ test. and match 'test' with the search string: this ( is \ a $ %A%. I do

  • Java RegEx - Illegal character dot 2011-11-30

    String filter = "a-zA-Z0-9äöüÄÖÜß-\\."; "^[^" + filter + "]*$", "" inputtext.replaceAll("^[^" + filter + "]*$", ""); This Java RegEx filter should remove all characters from inputtext except the characters in filter, but I'm getting an exception that

  • Java regex, need help with escape characters 2010-04-17

    My HTML looks like: <td class="price" valign="top"><font color= "blue">&nbsp;&nbsp;$&nbsp; 5.93&nbsp;</font></td> I tried: String result = ""; Pattern p = Pattern.compile("\"blue\">&nbsp;&nbsp;$&

  • java regex support for non-ascii values? 2009-05-27

    We have a current method which clears out chars that are not alphabetic or whitespace which is simply String clean(String input) { return input==null?"":input.replaceAll("[^a-zA-Z ]",""); } which really ought to be fixed to support non-english chars

  • Java: Regex on byte array 2009-09-06

    I want to do something like a regular expression in Java, but on a byte array instead of a String For example, let's say I want to delete from the array all continuous segments of 0's longer than 3 bytes byte a[] = {1,2,3,0,1,2,3,0,0,0,0,4}; byte r[]

  • Problem with % and / in Java regex 2010-07-09

    I have a problem with the % and / characters in Java regex. The following example will illustrate my issue: Pattern pattern = Pattern.compile("^[a-z]*[/%]$"); Matcher m = pattern.matcher("a%/"); System.out.println(m.find()); It prints "false" when I

  • Java RegEx meta character (.) and ordinary dot? 2010-09-09

    In Java RegEx, how to find out the difference between .(dot) the meta character and the normal dot as we using in any sentence. How to handle this kind of situation for other meta characters too like (*,+,/d,...) --------------Solutions-------------

  • How can I match series of | with a Java regex? 2010-09-26

    Couple of questions: 1) How to make the following regex which are based on search literal ^ work for the search literal | search literal ^ based regex (which works fine, which is one of the valuable inputs from this forum): String intermediateResult

  • Replacing character with File.separator using java.regex Pattern Matcher 2011-01-22

    I have a field called 'path' in back end database, which stores the path to certain resource. Instead of storing lots of backslashed (escaped) path for windows path, my idea is to let user enter the path with certain character as file separator (inde

  • Need java regex to match the below string 2011-02-01

    What is the java regex to match the below pattern? <anyString>.<5 or 10>.anyNumber.anyNumber Here 5 and 10 are numbers. --------------Solutions------------- .*\.(?:5|10)\.\d+\.\d+ should work. Explanation: .* # any number of characters (e

  • Java Regex, less than and more than sign 2011-04-13

    I have a string that users are able to enter on the internet, currently it is not protected against XSS attacks. I would like to be able to replace < and > symbols. Commonly known as 'less than', 'more than', 'angle brackets' etc. I am sure thi

  • Java regular expression and escaping meta characters 2011-04-24

    I am trying to write regexp for matching token embedded between two curly braces. For example if buffer Hello {World}, I want to get "World" token out of String. When I use regexp like \{*\} eclipse shows a error messages as Invalid escape sequence (

  • Javascript Regex to Java regex 2011-07-12

    I have the following regex in Javascript: /^(\{?(08)([3-9]){1}-([0-9]){7,7}\}?)$/ It matches numbers like: 087-1234893, 083-2839283, 086-4283944, etc. I've converted it to Java (Android) as follows: public boolean isValidMobilePhone(String phone){ bo

  • Java Regex pattern matching 2011-08-23

    I am developing a text highlighter in a file using the java regex pattern matching. Following is a code snapshot of it SearchQuery=preprocessedModifiedArrayList.get(i)+[\\w\\s\\W]*?"; pattern = Pattern.compile(SearchQuery); Matcher matcher = pattern.

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.112 (s). 11 q(s)