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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:26+00:00 2026-06-14T21:59:26+00:00

I have pattern/matcher lines that transform input Strings like this: 1 3 Hi [2

  • 0

I have pattern/matcher lines that transform input Strings like this:

1 3 Hi [2 1 4]

into an Array like this:

[0] => "1"
[1] => "3"
[2] => "Hi"
[3] => "2 1 4"

That’s the code:

String input = sc.nextLine();

Pattern p = Pattern.compile("(?<=\\[)[^\\]]+|\\w+");
Matcher m = p.matcher(input);
List<String> cIn = new ArrayList<String>();
while(m.find()) cIn.add(m.group());

Now I realised that sometimes I could get some negative values, inputs like 4 2 -1 2. Since the input is a String, i can’t really use any regular expression to get that negative value.

Below in the code, I use

Integer.parseInt(cIn.get(0)); 

to transform that string value into the Integer, that is actually what I need.

Could you figure a way that allows me to keep together the - char and the number char? Then I would just check if there’s the - char to transform the number and multiply it by -1. (If there’s a better way I’d be glad to hear).

As usual, excuse me for my English.

  • 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-14T21:59:27+00:00Added an answer on June 14, 2026 at 9:59 pm

    You absolutely can use a regular expression to capture negative numbers, but it depends on what you’re trying to weed out.

    "(?<=\\[)[^\\]]+|[-\\w]+"
    

    The simplest way is to simply add ‘-‘ to the group of recognized word characters. However, this will also result in weird formations like ‘9-9’ being legal. Considering you already match tokens like ‘9_9’, I’m not sure that’s a problem for you. I’d probably just add another alternation to the end of this regex:

    "(?<=\\[)[^\\]]+|\\w+|-?\\d+"
    

    Which allows an optional ‘-‘ character followed by at least one digit. Or, a negative number. This is fairly robust – you’re literally just defining an additional type of match (a very specific one), but every time you find a new case, you really shouldn’t just keep adding ‘|…’ to the end of your regex. It’s about the least efficient way to do what you’re doing. It seems, in your situation, that this isn’t really a problem, but you should think about this as your use case expands.

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

Sidebar

Related Questions

I have a string that contains multiple parameters delimited by #, like this :
I have a function that uses Pattern#compile and a Matcher to search a list
For one NSString, I have N pattern strings. I'd like to extract substrings around
Problem that i face: -I have an input string, a SQL statement that i
I would like to be able to have a pattern that matches only expressions
I have a string of text like this: This is a[WAIT] test. What I
I have the following method: protected BigDecimal stringToBigDecimal(String value) throws ParseException { if (Pattern.matches([\\d,.]+,
I have pattern like below hi hello hallo greetings salutations no more hello for
I have this pattern: ~^([a-z0-9]+[a-z0-9-]+[a-z0-9]+\.([a-z]+)(\.[a-z]+)?)$~i It will match the following: xxx.xxx or xxx.xxx.xxx (number
I have this pattern written ^.*\.(?!jpg$|png$).+$ However there is a problem - this pattern

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.