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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:04:29+00:00 2026-06-10T07:04:29+00:00

Am I misinterpreting something regarding Java regexes? Shouldn’t the following match the leading zero:

  • 0

Am I misinterpreting something regarding Java regexes? Shouldn’t the following match the leading zero:

public class Testit {
    public static void main(String[] args) {
        format("0115724848");
    }


    private static void format(String elementToFormat) {
        if (elementToFormat.matches("^0")) {
            System.out.println("leading zero:" + elementToFormat);
        } else {
            System.out.println("no leading zero:" + elementToFormat);
        }
    }
}
  • 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-10T07:04:30+00:00Added an answer on June 10, 2026 at 7:04 am

    matches tries to match the pattern against the whole of the input string… and your input string isn’t just “beginning of string followed by 0”.

    Either you need "0.*" (the ^ is unnecessary precisely because matches will match the whole string) or you could create a Pattern and then use:

    if (pattern.matcher(text).lookingAt())
    

    Of course it’s not clear why you’re using a regex here at all, in that you can use:

    if (text.startsWith("0"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure my use of the Random class is right--perhaps I am misinterpreting
The Documentation of the java.util.Properties-class states: A property list can contain another property list
I'm learning sml, and wrote the following simple function: (* Return a list with
I am reading through the Public Review Draft of the Web Beans specification (JSR-299)
I'd like to have a class with two array subscript operator overloads: one used
I'm relatively new to Python, and something is acting up. Basically, when I call
I am using Edit Html in firebug and inserting the following bit of code:
I wrote the following code. It tries to create a storyboard that does the
I may be misinterpreting the exact phrasing on the question about which I've been
I have two questions, stemming from observed behavior of C# static methods (which I

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.