0

Hi I want to check the String should contain a comma , in every 12 the character using Matcher and Pattern in java. I have used ^([a-zA-Z0-9\\-?\\,])+$ this pattern is not working for my requirement can anyone provide solution on this. Here is my code:

package com.ibm.rock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test{

      private Pattern pattern;
      private Matcher matcher;


 private static final String EMAIL_PATTERN ="^([a-zA-Z0-9]{11},)*[a-zA-Z0-9]{0,11}$";


      public Test(){
          pattern = Pattern.compile(EMAIL_PATTERN);
      }

      /**
       * Validate hex with regular expression
       * @param hex hex for validation
       * @return true valid hex, false invalid hex
       */
      public boolean validate(final String hex){
          System.out.println(hex.length());
          String value=hex.trim().replaceAll(" ","");


          matcher = pattern.matcher(value);
                  System.out.println("matcher:"+matcher);
          return matcher.matches();
          }else{
              return  false;
          }

      }
      public static void main(String[] args) {
        System.out.println("ProjectsValidation::"+new Test().validate("IN-07-00177 , IN-07-00533"));

      }
}
2
  • 1
    Can you elaborate a bit more? Are you trying to look for a specific character every 12th character? Or are you trying to look for a series of characters? An example of what you're trying to find would help a lot.
    – Michael
    Jan 30, 2012 at 13:22
  • Am looking for a ',' for every 12 th character in the String Jan 30, 2012 at 13:24

4 Answers 4

3

If you're only looking for every 12th character, you can keep it pretty simple:

/^(.{11},)*.{0,11}$/

If you want no commas except at the 12th positions, try

/^([^,]{11},)*[^,]{0,11}$/
4
  • Question is, if the String is "ok", OP wants it match or not? my understanding of the question is, he doesn't want it match. but your regex says ok.
    – Kent
    Jan 30, 2012 at 13:44
  • Thanks BryanH,This is working for every 12 th character should be , and even it should allow only [a-zA-Z0-9\\-] in before 12 the character,how to add this in regex pattern. Jan 30, 2012 at 13:44
  • Hi BryanH I have modified the given the regex pattern as follows ^([a-zA-Z0-9\\-^,]{11},)*[a-zA-Z0-9\\-^,]{0,11}$ and it's working...Thanks A lot. Jan 30, 2012 at 13:48
  • @kent you have a valid point. In that case, changing the first asterisk (match 0 or more) to a plus (match 1 or more) should do the trick.
    – BryanH
    Jan 30, 2012 at 14:05
0

My understanding of the question:

  • check every 12th character, it must be ",". even if it is the last character of the string.
  • the String should contain at least 12 characters, if less, not matched

so

"abcd"  - NOT Match
"12345678901,xx" - not match
"12345678901,10987654321" - not match
"12345678901" - not match
"12345678901,10987654321," match!

I hope I understood it correctly.

then:

^(\w{11},)+$

should work. note that the '\w' could be changed to fit your requirement.

1
  • Not sure if the requirement was the string length must be a multiple of 12 characters. If no, then I believe example two returns false unexpectedly.
    – BryanH
    Jan 30, 2012 at 14:10
0

So, if I understood correctly you want to check that in a particular String every 12-th element is a comma? If yes, then this simple regex will work:

String input = "qazwsxedcrf,1234567890 ,";
Pattern p = Pattern.compile("(.{11},)+");
Matcher m = p.matcher(input);
System.out.println(m.matches());
-1

Try this regex:

^([a-zA-Z...]{11},)*[a-zA-Z...]{0,11}$
3
  • 3
    Shouldn't it be {11} and {0,11} at the end?
    – biziclop
    Jan 30, 2012 at 13:29
  • @biziclop well spotted, fixed. Feb 1, 2012 at 7:50
  • @Kent Why is abc not a valid string? There was no requirement that the string contain any commas or have a minimum length, just that every 12th charaqcter is a comma. Feb 1, 2012 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.