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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:16:01+00:00 2026-06-15T03:16:01+00:00

In a regular expression, I need to know how to match one thing or

  • 0

In a regular expression, I need to know how to match one thing or another, or both (in order). But at least one of the things needs to be there.

For example, the following regular expression

/^([0-9]+|\.[0-9]+)$/

will match

234

and

.56

but not

234.56

While the following regular expression

/^([0-9]+)?(\.[0-9]+)?$/

will match all three of the strings above, but it will also match the empty string, which we do not want.

I need something that will match all three of the strings above, but not the empty string. Is there an easy way to do that?

UPDATE:

Both Andrew’s and Justin’s below work for the simplified example I provided, but they don’t (unless I’m mistaken) work for the actual use case that I was hoping to solve, so I should probably put that in now. Here’s the actual regexp I’m using:

/^\s*-?0*(?:[0-9]+|[0-9]{1,3}(?:,[0-9]{3})+)(?:\.[0-9]*)?(\s*|[A-Za-z_]*)*$/

This will match

45
45.988
45,689
34,569,098,233
567,900.90
-9
-34 banana fries
0.56 points

but it WON’T match

.56

and I need it to do this.

  • 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-15T03:16:02+00:00Added an answer on June 15, 2026 at 3:16 am

    The fully general method, given regexes /^A$/ and /^B$/ is:

    /^(A|B|AB)$/
    

    i.e.

    /^([0-9]+|\.[0-9]+|[0-9]+\.[0-9]+)$/
    

    Note the others have used the structure of your example to make a simplification. Specifically, they (implicitly) factorised it, to pull out the common [0-9]* and [0-9]+ factors on the left and right.

    The working for this is:

    • all the elements of the alternation end in [0-9]+, so pull that out: /^(|\.|[0-9]+\.)[0-9]+$/
    • Now we have the possibility of the empty string in the alternation, so rewrite it using ? (i.e. use the equivalence (|a|b) = (a|b)?): /^(\.|[0-9]+\.)?[0-9]+$/
    • Again, an alternation with a common suffix (\. this time): /^((|[0-9]+)\.)?[0-9]+$/
    • the pattern (|a+) is the same as a*, so, finally: /^([0-9]*\.)?[0-9]+$/
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression able to match everything but a string starting with
I need to know if there is a regular expression for testing for the
I need a regular expression to match phone numbers. I just want to know
I need a regular expression that will match the first letter of a song
I need a regular expression to help me make a match in my string.
I need a regular expression that will match any string containing at most 2
I'm trying to work out what regular expression I would need to change this
Hi I need a regular expression that'll give me something like this part ./something\,
I need a regular expression that will not allow multiple special characters in a
I need a regular expression that will accept well-formed emails in several formats (see

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.