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 8620979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:42:44+00:00 2026-06-12T06:42:44+00:00

This simple regex program import java.util.regex.*; class Regex { public static void main(String []

  • 0

This simple regex program

import java.util.regex.*;
class Regex {
    public static void main(String [] args) {
        System.out.println(args[0]); // #1
        Pattern p = Pattern.compile(args[0]); // #2
        Matcher m = p.matcher(args[1]);
        boolean b = false;
        while(b = m.find()) {
            System.out.println(m.start()+" "+m.group());
        }
    }
}

invoked by java regex "\d" "sfdd1" compiles and runs fine.

But if #1 is replaced by Pattern p = Pattern.compile("\d");, it gives compiler error saying illegal escape character. In #1 I also tried printing the pattern specified in the command line arguments. It prints \d, which means it is just getting replaced by \d in #2.

So then why won’t it throw any exception? At the end it’s string argument that Pattern.compile() is taking, doesn’t it detect illegal escape character then? Can someone please explain why is this behaviour?

  • 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-06-12T06:42:46+00:00Added an answer on June 12, 2026 at 6:42 am

    A backslash character in a string literal needs to be escaped (preceded by a backslash). When passed in from the command line the string is not a string literal. The compiler complains because "\d" is not a valid escape sequence (see Escape Sequences for Character and String Literals ).

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

Sidebar

Related Questions

public class PatternTest { public static void main(String[] args) { System.out.println(117_117_0009v0_172_5738_5740.matches(^([0-9_]+v._.))); } } This
This simple regex matching returns a string instead of an object on every browser
Simple regex question. I have a string on the following format: this is a
Really got stuck on this simple regex. Need it to validate a string, that
I'm trying to write a simple regex for my java program to count syllables,
I have a simple program where the user can enter a string. After this
I have this relatively simple regex for usernames // Enforce that username has to
This sounds simple but my regex knowledge is limited. I need an expression to
Let's say I have two strings like this: XABY XBAY A simple regex that
Simple question but I got a headache to solve this game. Example regex. [a-zA-Z0-9\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.