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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:21:57+00:00 2026-05-13T12:21:57+00:00

I’m building a small Java library which has to match units in strings. For

  • 0

I’m building a small Java library which has to match units in strings. For example, if I have “300000000 m/s^2”, I want it to match against “m” and “s^2”.

So far, I have tried most imaginable (by me) configurations resembling (I hope it’s a good start)

"[[a-zA-Z]+[\\^[\\-]?[0-9]+]?]+"

To clarify, I need something that will match letters[^[-]numbers] (where [ ] denotes non obligatory parts). That means: letters, possibly followed by an exponent which is possibly negative.

I have studied regex a little bit, but I’m really not fluent, so any help will be greatly appreciated!

Thank you very much,

EDIT:
I have just tried the first 3 replies

String regex1 = "([a-zA-Z]+)(?:\\^(-?\\d+))?";
String regex2 = "[a-zA-Z]+(\\^-?[0-9]+)?";
String regex3 = "[a-zA-Z]+(?:\\^-?[0-9]+)?";

and it doesn’t work… I know the code which tests the patterns work, because if I try something simple, like matching “[0-9]+” in “12345”, it will match the whole string. So, I don’t get what’s still wrong. I’m trying with changing my brackets for parenthesis where needed at the moment…

CODE USED TO TEST:

public static void main(String[] args) {
    String input = "30000 m/s^2";

//    String input = "35345";

    String regex1 = "([a-zA-Z]+)(?:\\^(-?\\d+))?";
    String regex2 = "[a-zA-Z]+(\\^-?[0-9]+)?";
    String regex3 = "[a-zA-Z]+(?:\\^-?[0-9]+)?";
    String regex10 = "[0-9]+";
    String regex = "([a-zA-Z]+)(?:\\^\\-?[0-9]+)?";
    Pattern pattern = Pattern.compile(regex3);
    Matcher matcher = pattern.matcher(input);

    if (matcher.matches()) {
        System.out.println("MATCHES");
        do {
            int start = matcher.start();
            int end = matcher.end();
//            System.out.println(start + " " + end);
            System.out.println(input.substring(start, end));
        } while (matcher.find());
    }

}
  • 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-13T12:21:57+00:00Added an answer on May 13, 2026 at 12:21 pm

    You’re mixing the use of square brackets to denote character classes and curly brackets to group. Try this instead:

    [a-zA-Z]+(\^-?[0-9]+)?
    

    In many regular expression dialects you can use \d to mean any digit instead of [0-9].

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

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In case the captured stream is already uncompressed AVI you… May 13, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer The command you're looking for is: ssh-add C:\path\to\key First, you… May 13, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer I've been referring to this: http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-part-6_25.html May 13, 2026 at 8:21 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.