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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:15:48+00:00 2026-05-26T19:15:48+00:00

I am trying to convert the following regular expression from Java to .NET: (?i:(?:([^\d,]+?)\W+\b((?:CA|SD|SC|CT|DC)\b)?\W*)?(\d{5}(?:[-

  • 0

I am trying to convert the following regular expression from Java to .NET:

    (?i:(?:([^\d,]+?)\W+\b((?:CA|SD|SC|CT|DC)\b)?\W*)?(\d{5}(?:[- ]\d{3,4})?)?)

When I run a match against the following string:

Mountain View, CA 94043

using a Pattern and Matcher object in Java, it populates four groups with the values:

    "Mountain View, CA 94043"
    "Mountain View"
    "CA"
    "94043"

However, in .NET, there are two matches. The first match populates the four groups with these values:

    "Mountain "(there is a space on the end of group 0)
    "Mountain"
    ""
    ""

The second match populates the three groups with these values:

    "View, CA 94043"
    "View"
    "CA"
    "94043"

I also tried the expression using RegexBuddy using both the Java and .NET modes and in RegexBuddy, both modes work like the .NET version.

Thanks everyone!

  • 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-26T19:15:48+00:00Added an answer on May 26, 2026 at 7:15 pm

    Add ^ to the beginning of your pattern, and add $ to the end of it to match the beginning and end of the string, respectively. This will make the pattern match the entire string and produces your desired result:

    string input = "Mountain View, CA 94043";
    string pattern = @"^(?i:(?:([^\d,]+?)\W+\b((?:CA|SD|SC|CT|DC)\b)?\W*)?(\d{5}(?:[- ]\d{3,4})?)?)$";
    
    Match m = Regex.Match(input, pattern);
    
    foreach (Group g in m.Groups)
    {
        Console.WriteLine(g.Value);
    }
    

    Since you didn’t restrict the pattern to be an exact match, as above, it found partial matches, especially since some of your groups are completely optional. Thus, it considers “Mountain” a match, then considers “View, CA 94043” as the next match.

    EDIT: as requested in the comments, I’ll try to point out the differences between the Java and .NET regex approaches.

    In Java the matches() method returns true/false if the pattern matches the whole string. Thus it doesn’t require the pattern to be modified with boundary anchors or atomic zero-width assertions. In .NET there is no such equivalent method that will do this for you. Instead, you need to explicitly add either the ^ and $ metacharacters, to match the start and end of the string or line, respectively, or the \A and \z metacharacters to do the same for the entire string. For a reference of .NET metacharacters check out this MSDN page. I’m not sure which set of anchors Java’s matches() uses, although this article suggests \A and \z are used.

    Java’s matches() returns a boolean, and .NET provides the Regex.IsMatch() method to do the same thing (apart from the already discussed difference of matching the entire string). The .NET equivalent of Java’s find() method is the Regex.Match() method, which you can use in a loop to continue to find the next match. In addition, .NET offers a Regex.Matches() method that will do this for you, and returns a collection of successful matches. Depending on your needs and the input this might be fine, but for added flexibility you may want to check Match.Success in a loop and use the Match.NextMatch() method to keep looking for matches (an example of this is available in the NextMatch link).

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

Sidebar

Related Questions

i'm trying to convert the following code from Java to C#. // Replace 0
I'm trying to create a string from a regular expression. I noticed that in
I'm trying to convert the following code from C# to Java, but I get
I am trying to convert the following LINQ code from C# to VB.NET. Here
I am trying to convert the following SQL into a LINQ expression SELECT COUNT(ID)
I'm tring to convert the following 3 methods from java-actionscript to Objective C. Part
I am trying to convert the following java binary search routine to as3. I
How to extract and save array from string parameter? I'm trying convert string beafore_create
I am trying to convert string into NSDate using following code. NSString *old =
I'm trying to convert the following code from OpenGL 1.5 spec. to the OpenGLES

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.