Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import java.util.Scanner;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class D_TheNextLevelProblem {
   public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter a username: ");
        String username = sc.nextLine();
      int testCases = Integer.parseInt(in.nextLine());
      while(testCases>0){
         String pattern = "^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$";
           Pattern r = Pattern.compile(pattern);
           Matcher m = r.matcher(username);
         if (m.find( )) {
            System.out.println("Valid");
         } else {
            System.out.println("Invalid");
         }
         testCases--;
      }
   }
}



This was the full info of what we we're gonna do sorry for not putting it

An Instagram username is limited to 30 characters and must contain only letters, numbers, periods, and underscores. Accept a string from a scanner, formulate a regular expression that complies with these rules, and write a program that checks if the string matches the regular expression that you formulated. Output "Valid Username" if it is valid, and "Invalid Username" if otherwise.


What I have tried:

None, I couldn't understand it my little brain won't work
Posted
Updated 5-Feb-22 1:59am
v2

The regex you show doesn't work - it isn't complete, probably because it's got a named capture group or similar that has been "swallowed" by your unformatted posting.

Next time, try using the "code block" option when you paste code and it will be formatted, indented, and (probably) syntax highlighted correctly, as well as knowing what is meant to be a HTML tag and what is meant to be code ...

But we can't help you fix a regex without knowing what it is supposed to do, and seeing sample input and output - what you expect, and what you get. We can't do that just as a garage can't fix your car is you just call them and say "it don't work"!

But regexes are complicated beasts at the best of times, and if you wrote that one you should understand what it is supposed to do, and how it works - if you didn't or you don't then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.

Feed it your regex and sample data, and it'll both explain it to you and help you correct it.

If that doesn't solve your problem, then we'd need a lot more info to help you at all!
 
Share this answer
 
Now we have more info, it's pretty clear that this is your homework, and the Regex you show has nothing to do with the problem as stated.

Read the assignment carefully.
1) The maximum length of the username is 30 characters. So start with that and come up with a basic regex that accepts any character but will not match 31+.
That's easy - so easy I'll give it to you:
RegEx
^.{1,30}$
2) Only the following are allowed: letters, numbers, periods, underscores.
That's also easy: there is a "specified set" you can use in a regex to limit it to "just these":
RegEx
[+-!]
will match only a single plus, minus, or exclamation mark.
If you have a look at "design mode" in Expresso, it'll show you how to use it and some of the special characters it can contain.

But I'm not doing it for you! While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!

Hint: my version of the correct regex was 14 characters long ... yours may be longer!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900