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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:54:57+00:00 2026-05-26T16:54:57+00:00

I have an input string that will follow the pattern /user/<id>?name=<name> , where <id>

  • 0

I have an input string that will follow the pattern /user/<id>?name=<name>, where <id> is alphanumeric but must start with a letter, and <name> is a letter-only string that can have multiple spaces. Some examples of matches would be:

/user/ad?name=a a
/user/one111?name=one ONE oNe
/user/hello?name=world

I came up with the following regex:

String regex = "/user/[a-zA-Z]+\\w*\\?name=[a-zA-Z\\s]+";

All of the above examples match the regex, but it only looks at the first word in <name>. Shouldn’t the sequence \s allow me to have white spaces?

The code that I made to test what it is doing is:

String regex = "/user/[a-zA-Z]+\\w*\\?name=[a-zA-Z\\s]+";
// Check to see that input matches pattern
if(Pattern.matches(regex, str) == true){
   str = str.replaceFirst("/user/", "");
   str = str.replaceFirst("name=", "");
   String[] tokens = str.split("\\?");
   System.out.println("size = " + tokens.length);
   System.out.println("tokens[0] = " + tokens[0]);
   System.out.println("tokens[1] = " + tokens[1]);
} else
    System.out.println("Didn't match.");

So for example, one test might look like:

/user/myID123?name=firstName LastName
size = 2
tokens[0] = myID123
tokens[1] = firstName

whereas the desired output would be

tokens[1] = firstName LastName

How can I change my regex 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-05-26T16:54:58+00:00Added an answer on May 26, 2026 at 4:54 pm

    Not sure what you think is the problem in your code. tokens[1] will indeed contain firstName LastName in your example.

    Here’s an ideone.com demo showing this.


    However, have you considered using capturing groups for the id and the name.

    If you write it like

    String regex = "/user/(\\w+)\\?name=([a-zA-Z\\s]+)";
    
    Matcher m = Pattern.compile(regex).matcher(input);
    

    you can get hold of myID123 and firstName LastName through m.group(1) and m.group(2)

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

Sidebar

Related Questions

I have the following C# which simply replaces parts of the input string that
I have a string input that i do not know whether or not is
I have a string input = "maybe (this is | that was) some ((nice
I have an input field whose name is an MD5 string e.g.: <input type=hidden
I have a string value from a user input box. I have to figure
I have an input string that's generated as in following example: string.Format(Document {0}, was
I have a string that will look something like this: I'm sorry the code
I have a program that get's input string with file path in one JTextArea
I have an array of input strings that contains either email addresses or account
I have an algorithm that generates strings based on a list of input words.

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.