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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:37:29+00:00 2026-05-16T00:37:29+00:00

I am trying to match inputs like <foo> <bar> #####<foo> #####<bar> I tried #{5}?<\w+>

  • 0

I am trying to match inputs like

<foo>
<bar>
#####<foo>
#####<bar>

I tried #{5}?<\w+>, but it does not match <foo> and <bar>.

What’s wrong with this pattern, and how can it be fixed?

  • 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-16T00:37:30+00:00Added an answer on May 16, 2026 at 12:37 am

    On ? for optional vs reluctant

    The ? metacharacter in Java regex (and some other flavors) can have two very different meanings, depending on where it appears. Immediately following a repetition specifier, ? is a reluctant quantifier instead of “zero-or-one”/”optional” repetition specifier.

    Thus, #{5}? does not mean “optionally match 5 #“. It in fact says “match 5 # reluctantly”. It may not make too much sense to try to match “exactly 5, but as few as possible”, but this is in fact what this pattern means.


    Grouping to the rescue!

    One way to fix this problem is to group the optional pattern as (…)?. Something like this should work for this problem:

    (#{5})?<\w+>
    

    Now the ? does not immediately follow a repetition specifier (i.e. *, +, ?, or {…}); it follows a closing bracket used for grouping.

    Alternatively, you can also use a non-capturing group (?:…)in this case:

    (?:#{5})?<\w+>
    

    This achieves the same grouping effect, but doesn’t capture into \1.

    References

    • regular-expressions.info
      • Question Mark for Optional – yes, but only with proper placement
      • Brackets for Grouping
      • Repetition
      • Flavor comparison
    • java.util.regex.Pattern: X{n}? : X, exactly n times

    Related questions

    • regex{n,}? == regex{n} ? (absolutely NOT!)
    • Difference between .*? and .* for regex

    Bonus material: What about ??

    It’s worth noting that you can use ?? to match an optional item reluctantly!

        System.out.println("NOMZ".matches("NOMZ??"));
        // "true"
    
        System.out.println(
              "NOM NOMZ NOMZZ".replaceAll("NOMZ??", "YUM")
        ); // "YUM YUMZ YUMZZ"
    

    Note that Z?? is an optional Z, but it’s matched reluctantly. "NOMZ" in its entirety still matches the pattern NOMZ??, but in replaceAll, NOMZ?? can match only "NOM" and doesn’t have to take the optional Z even if it’s there.

    By contrast, NOMZ? will match the optional Z greedily: if it’s there, it’ll take it.

        System.out.println(
              "NOM NOMZ NOMZZ".replaceAll("NOMZ?", "YUM")
        ); // "YUM YUM YUMZ"
    

    Related questions

    • method matches not work well
      • unlike other flavors, Java matches a pattern against the entire String
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to match a string against a pattern, but there's one thing I
In the following input string: {$foo}foo bar \\{$blah1}oh{$blah2} even more{$blah3} but not{$blarg}{$why_not_me} I am
Im trying to match forum_id with several different forum_id's, something like forum_id = 5,7,12,43,63,78
I'm trying to match the '12345' from a url in this form: http://domain.com/folder/title_of_this_12345 So
I am trying to validate inputs that can be altered by users in one
Im trying to match this syntax: pgm ::= exprs exprs ::= expr [; exprs]
I'm trying to validate my inputs with jQuery before a form is sent. This
Lets say i have a piece of code like this: test pattern | pattern
I'm trying to achieve a layout in html that looks exactly like this when
I am trying to match words driven from a database in a string input

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.