55

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

"\d{4}\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

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}"
));
3
  • The pattern is definitly legal for java, it works in eclipse. Sorry im not with NetBeans. Commented Sep 4, 2009 at 13:19
  • Interesting. I'm adding the entire line of code to my question and I'll try to build, even with the error...let's see what happens. Commented Sep 4, 2009 at 13:23
  • 4
    You use it in string, think of escaping \d and \w with \\d and \\w Commented Sep 4, 2009 at 13:25

7 Answers 7

98

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

"\\d{4}\\w{3}(0[1-9]|[12][0-9]|3[01])([01][0-9]|2[0-3])([0-5][0-9]){2}"

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);
    System.out.println(s);
    System.out.println(m.matches());
}

\\ //JUST TO MATCH TWO SLASHES :(
true
Sign up to request clarification or add additional context in comments.

Comments

15

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 :)

1 Comment

The way I see it, you got downvoted for not making effective use of SO's code-formatting capability. ;)
9

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}

2 Comments

This gets rid of the error...let's see if it actually works as a regex.
It appears to be working. +1. However, I accepted butterchicken's answer because it's more in-depth. Thanks for your help, though.
3

Have you tried this?

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

Comments

1

I had a similar because I was trying to escape characters such as -,*$ which are special characters in regular expressions but not in java.

Basically, I was developing a regular expression https://regex101.com/ and copy pasting it to java.

I finally realized that because java takes regex as string literals, the only characters that should be escaped are the special characters in java ie. \ and "

So in this case \\d should work. However, anyone having a similar problem like me in the future, only escaped double quotes and backslashes.

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
0

all you need to do is to put

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

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

Comments

-1

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.

1 Comment

@MystikSpiral: \d and \w are character classes. They're shorthands (not "shortcuts") for the predefined character classes [0-9] and [A-Za-z0-9_] respectively. The brackets are redundant unless you're using them in combinations, like [\d\s] or [\w,.!?].

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.