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

  • Home
  • SEARCH
  • 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 6154625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:17:41+00:00 2026-05-23T20:17:41+00:00

Java provides some useful character classes like \d and \w . Can I define

  • 0

Java provides some useful character classes like \d and \w. Can I define my own character classes? For example, it would be useful to be able to define shorthands for character classes like [A-Za-z_].

  • 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-23T20:17:42+00:00Added an answer on May 23, 2026 at 8:17 pm

    Can I define my own character classes?

    No, you can’t.

    Personally, when I have a (slightly) complicated regex, I break the regex up in smaller sub-regexes and then “glue” them together with a String.format(...) like this:

    public static boolean isValidIP4(String address) {
        String block_0_255 = "(0|[1-9]\\d|2[0-4]\\d|25[0-5])";
        String regex = String.format(
                "%s(\\.%s){3}", 
                block_0_255, block_0_255
        );
        return address.matches(regex);
    }
    

    which is far more readable than a single pattern:

    "(0|[1-9]\\d|2[0-4]\\d|25[0-5])(\\.(0|[1-9]\\d|2[0-4]\\d|25[0-5])){3}"
    

    Note that this is just a quick example: validating IP addresses can probably better be done by a class from the java.net package, and if you’d do it like that, the pattern should be placed outside the method and pre-compiled.

    Be careful with % signs inside your pattern!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you provide an example of a byte buffer transferred between two java classes
Can someone please provide an example of creating a Java ArrayList and HashMap on
Is there a good lightweight framework for java that provides the publish/subscribe pattern? Some
I'm writing a web application in Java where at some point user can enter
I would like to get a list of classes that are available at runtime
I was going through some java code(which uses playframework ) that provides some shopping
Ant Java task provides fork parameter, which, by definition if enabled triggers the class
I'm having trouble searching for a decent Java library that provides Markov chains, and
I was curious if anyone had any suggestions on a Java library that provides
Java has generics and C++ provides a very strong programming model with template s.

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.