Time remaining:
Java Regex Patterns

Programming
Tutor: None Selected Time limit: 0 Hours

I currently have this following java regex pattern: C[0-9A-Z]{9}\\s 

It currently matches substring's in a string that begin with C only contain 0-9 and A-Z, is 10 characters long including the C and has a space after it. I need it to limit its matches to something that MUST include atleast one digit as well. How can i add this into my regex?

Nov 30th, -0001
\d+ in regex means 1 or more numbers. So in your case: C[A-Z]\d+{9}\\s
Jun 11th, 2013

Supplement: \d means [0-9], \d+ means at least one.

Jun 11th, 2013

This doesn't fulfill my requirements though because this means the 2nd character has to be a letter from A-Z

Jun 11th, 2013

You need positive lookahead for this:

C(?=.*[0-9])[A-Z0-9]+{9}
(?=.*[0-9]) means that you have at least one digit,  no matter the position.

Jun 11th, 2013

I used this in my code, and it still picks up CHDJSKDJFU which contains no digits

Jun 11th, 2013

This is my code:

import java.util.*;

import java.io.*;

import javax.swing.*;

import java.util.regex.*;

class testSite{        

  public static void main (String [] args){

    String testString = " CHDJSKDJFU CD73F98298 CLASSIFICATION jghfjtug94 CHDJ8394H CDJSKDIEHF CRJ839HDJK HDJDHJD 3787383 hdhdhdh";

    String patternStr = "C(?=.*[0-9])[A-Z]+{9}"; 

    Pattern pattern = Pattern.compile(patternStr);

    Matcher matcher = pattern.matcher(testString);

   

    if(matcher.find()){

     

     

      for (int i =matcher.start(); i < testString.length(); i++){

        char[] charArray = testString.toCharArray();

        System.out.print(charArray[i]);

      }

      System.out.println();

     

    }

  }

}


Jun 11th, 2013

when I use [A-Z0-9] it does the exact same thing

Jun 11th, 2013

Is the limit {9} working?

Jun 11th, 2013

No it is not

Jun 11th, 2013

it is even picking up CLASSIFICATION which contains more than 10 char and contains no numbers

Jun 11th, 2013

(?={9})C(?=.*[0-9])[A-Z0-9]+
 What about this.

Jun 11th, 2013

still picks up CLASSIFICATION

Jun 11th, 2013

Have you every tried some very simple regex like simply {9} ?

Jun 11th, 2013

Yes. I can get it to the point where it fulfils all requirements BUT must include a digit

Jun 11th, 2013

So {9} works fine? Idk why the positive lookahead is not working but you can try to test them seperately

First test {9} to see if the length is correct.

Then test if there is a number:

String strWithNumber ="This string has a 1 number";

if(strWithNumber.matches(".*\\d.*")){System.out.println("'"+strWithNumber+"' contains digit");

}else{

System.out.println("'"+strWithNumber+"' does not contain a digit");}

String strWithoutNumber ="This string does not have a number";

if(strWithoutNumber.matches(".*\\d.*")){

System.out.println("'"+strWithoutNumber+"' contains digit");

}else{System.out.println("'"+strWithoutNumber+"' does not contain a digit");}
Output:

'This string has a 1 number' contains digit
'This string does not have a number' does not contain a digit

Jun 11th, 2013

Just figured out what the lookahead is doing. It is only finding matches if the entire string has atleast one digit not the pattern in the string. for example, using this regex: "\\sC((?=.*[0-9])[A-Z0-9]{9,9})\\s"

In this string,  "5CLASSCIFICATION JDJDJSUCHD CJJDJDJDJD JDISDDJFHRY CSGDHEYRHDT CJSKDJFYEHS"

it will match  CJJDJDJDJD

but in this string, "CLASSCIFICATION JDJDJSUCHD CJJDJDJDJD JDISDDJFHRY CSGDHEYRHDT CJSKDJFYEHS"

it wont match anything.

What I need it to do it match something like CRJ837HDJD but not CRJFHGJKDJ

Jun 11th, 2013

1. Have you tried to use simple regex to match your string? Let me ask you again, is {9} only working for you or not(regardless your requirement, does it even return all 9 chars patterns?) If it works, try use the way i told you in the last reply and combine simple regex all together. I posted about how to determine a pattern has digit or not.

2. If even {9} is not working. Why not try to break the string into several strings and just "regex" every string?


Jun 11th, 2013

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
...
Nov 30th, -0001
...
Nov 30th, -0001
Dec 7th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer