Results 1 to 5 of 5
  1. #1
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Regex giving me a hard time

    Hello
    I am trying to match validate user input via regex comparison. But i fail to do so.
    The string that will be inserted will have the following pattern:
    "digit/s " and it can be repeated a random number of times.
    The program will extend to characters in the future but for now i am struggling with digits.
    i am using a StringBuilder to build the regex string. Or maybe i could use something else but so far i didn't make it.
    So far i have this regex
    Java Code:
    "(\\d+\\s+)+"
    . The problem is that the newline character will interfere with the last number.
    So if i enter
    Java Code:
    "3 4 555"
    - it will not match. And the regex will only see 3 4.
    But if i enter
    Java Code:
    "3 4 555 "
    - with an extra space at the end it works. Obviously that is not the desired case.
    I did find a workaround, but i fear it is somewhat broken
    I changed to
    Java Code:
    "(\\d+\\s*)+"
    . But in this case the pattern should see 3 4 5 5 5 as the regular expression. Am i correct?
    I also tried some other variants but none of them worked:
    Java Code:
    "\\d+(\\s+|\\n*)"
    or i could do something like
    Java Code:
    "(\\d+\\s){" + i + "}"
    - where i is the number of values i enter.
    Am i on the right track here? What am i forgetting?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Regex giving me a hard time

    How are you getting your input. From Scanner? From a file? Can you show your code?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Regex giving me a hard time

    Scanner.in

    Java Code:
       Scanner input = new Scanner(System.in);
    		for (int i = 0; i < numarPerechi; i++) {
    			System.out.println("Introduceti perechea " + (i + 1)
    					+ " de numere separate de spatiu:");
    
    			Pattern myPattern = Pattern.compile("(\\d+\\s*)+");
    			String pachet = input.nextLine();
    			Matcher m = myPattern.matcher(pachet);
    			boolean b = m.matches();
    
    			if (b) {
    				Integer[] perecheNumere = new Integer[numarValoriInPereche];
    				StringTokenizer st = new StringTokenizer(pachet);
    				for (int j = 0; j < numarValoriInPereche; j++) {
    
    					perecheNumere[j] = Integer.parseInt(st.nextToken());
    				}
    				this.lista = new ArrayList<Integer[]>();
    				lista.add(perecheNumere);
    				// System.out.println(Arrays.toString(perecheNumere));
    			}
    
    		}
    		input.close();
    numarPerechi = is the number of items that will be inserted

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Regex giving me a hard time

    The scanner nextLine method ignores any line termination character (check out the Scanner API documentation). So you need to check for end of line as an option. End of line is represented by $.

    "(\\d+(\\s+|$))+"

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Having a hard time figuring this out! Please help.
    By syngine90 in forum New To Java
    Replies: 2
    Last Post: 12-19-2012, 11:31 AM
  2. HELP me with Binary Search Tree. hard time on it.
    By xajaxworldx in forum Advanced Java
    Replies: 4
    Last Post: 05-06-2011, 06:59 AM
  3. Static (having a hard time)
    By maknib in forum New To Java
    Replies: 4
    Last Post: 11-02-2010, 08:05 AM
  4. Finding Regex hard to learn
    By Dan0100 in forum New To Java
    Replies: 2
    Last Post: 09-21-2010, 03:18 PM
  5. Extreme nooby having hard time with this app
    By asterix350z in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •