Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:04:03+00:00 2026-05-25T13:04:03+00:00

I recently came across some odd behavior that involves Java’s regex engine. When writing

  • 0

I recently came across some odd behavior that involves Java’s regex engine.

When writing some validation, I needed to add square brackets to my regex, like so:

"[^a-zA-Z0-9_/.@ ]"  // original expression
"[^a-zA-Z0-9_/.@ /]/[]"  // first modificiation

However… this implementation failed. After experimentation, I discovered that it would then work if I moved the space char to the end.

"[^a-zA-Z0-9_/.@/]/[ ]"  // final working modification

Now the calling code that used this expression used the String.replaceAll(String, String) method, as listed here.

My question is… does anyone have any good technical idea on why the placing of the space alters the meaning of this regex? It really shouldn’t matter.

[EDITED]
From the comments and answers–this is an example where using the built-in String method leads to incorrect behavior that is NOT caught. My Runtime environment does NOT complain at all even though if you read the documentation on String.replaceAll(String, String) it clearly states that it is the same functionality as Pattern.compile(regex).matcher(str).replaceAll(repl) I think I will file a bug.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:04:04+00:00Added an answer on May 25, 2026 at 1:04 pm

    You use the wrong escaping character, it’s \ and not /.

    Also, I’m not sure if you wanted your character group to include / and . or if you thought that . needs to be escaped in character groups (it doesn’t need to be escaped: it always represents the literal . in character groups).

    When trying to compile [^a-zA-Z0-9_/.@ /]/[] it gives this exception:

    java.util.regex.PatternSyntaxException: Unclosed character class near index 20
    [^a-zA-Z0-9_/.@ /]/[]
                        ^
        at java.util.regex.Pattern.error(Pattern.java:1713)
        at java.util.regex.Pattern.clazz(Pattern.java:2254)
        at java.util.regex.Pattern.sequence(Pattern.java:1818)
        at java.util.regex.Pattern.expr(Pattern.java:1752)
        at java.util.regex.Pattern.compile(Pattern.java:1460)
        at java.util.regex.Pattern.(Pattern.java:1133)
        at java.util.regex.Pattern.compile(Pattern.java:823)
    

    This indicates that there is a problem with the character class at that point. And in fact: you’ve got an empty character class [] which is not valid!

    [^a-zA-Z0-9_/.@ /]/[] means “a character not matching (a-z, A-Z, 0-9, _, /, ., @, or /), followed by a slash / followed by <fails to compile because it is malformed>”.

    What you want is probably [^a-zA-Z0-9_.@ \]\[] which is “a character not matching a-z, A-Z, 0-9, _, ., @, , ] or [“.

    If you write it in a String literal remember to double the \ (because they have special meanings in String literals as well!):

    Pattern regex = Pattern.compile("[^a-zA-Z0-9_.@ \\]\\[]");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across some Java code that simply put some strings into a
An interesting issue came up recently. We came across some code that is using
I was reading some Java recently and came across something (an idiom?) new to
I was working on some code recently and came across a method that had
I recently came across some code that looked like: if(sizeof(var,2) == 4) { ...
I recently came across some weird looking class that had three constructors: class Class
I recently came across this in some code - basically someone trying to create
I recently came across an IE7 only bug that I thought I'd share so
I recently came across a ASP 1.1 web application that put a whole heap
We recently came across some sample code from a vendor for hashing a secret

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.