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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:50:38+00:00 2026-05-28T06:50:38+00:00

In the following let’s say zip codes I am trying to exclude the 33333-

  • 0

In the following let’s say zip codes I am trying to exclude the 33333- from the result.
I do:

String zip = "11111 22222 33333- 44444-4444";
String regex = "\\d{5}(?(?=-)-\\d{4})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(zip);
while (matcher.find()) { 
   System.out.println(" Found: " + matcher.group());     
}

Expect to get:

Found:  11111  
Found:  22222  
Found:  44444-4444

I am trying to enforce format of:
5 digits optionally followed by a – and 4 digits. 5 digits with just a – (hyphen) is not wanted

I get exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unknown inline modifier near index 7
\d{5}(?(?=-)(-\d{4}))
       ^
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.group0(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)

Am I not using the conditional lookahead correctly?

  • 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-28T06:50:39+00:00Added an answer on May 28, 2026 at 6:50 am

    To capture all numbers except 33333 use this code:

    String zip = "11111 22222 33333- 44444-4444";
    String regex = "\\d{5}(?=(-\\d{4}|\\s|$))(-\\d{4})?";
    Matcher m = Pattern.compile(regex).matcher(zip);
    while(m.find())
        System.out.printf("Macthed: [%s]%n", m.group(1));
    

    OUTPUT:

    Macthed: [11111]
    Macthed: [22222]
    Macthed: [44444-4444]
    

    Explanation: This RegEx is using lookahead that itself is like a condition, which means match 5 digit number which must be followed by – and 4 digits OR a space OR end of string and then it is optionally matching a text – and 4 digits.

    The reason why your original RegEx is throwing exception because there is a syntax error in ?:(?=-) part of your RegEx.

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

Sidebar

Related Questions

I'm trying to learn interfaces and want to try the following: Let's say I
I'm wondering if anyone knows how to accomplish the following: Let's say I have
I'm trying to figure out what's the best method to do the following: Let's
I'm looking to do the following : let's say I have a number like
I am looking for the most elegant way to do the following: Let's say
Does C# allow the following thing: Let's say I've got namespace X and namespace
I'm facing a hard time to get the following query: Let's say I have
Given the following: let f<'a,'b> = typeof<'a>.Name, typeof<'b>.Name //val f<'a,'b> : string * string
I'm stuck with trying to create a function that will do the following: let
I'm checking my form with RSV validator. Want to get work following: Let's say

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.