3

I want to validate an expression. I could able to achieve 90% however I am failing on one condition. How we can add an expression to make sure that particular character after few number of character should be an alphabet and next if any should be a number.

Eg: [A-Z1-9]{1,30}?[A-Z]{0,1}$[1-9]{0,1}

The pattern can have max 32 characters and last 2 characters are optional If the character exceeds 30 it should starts with an alphabet [A-Z] and it should occur only once {0,1} And the 32nd character should be a number [1-9] and it should occur only once and should present if 31st char exists Could you help me please ?

5
  • To rephrase: your string will either be 30 or 32 characters long. If it is 32 character long, then the 31st will be upper-case alphabetic and the 32nd will be a digit? May 12, 2014 at 7:17
  • It will be great if you can provide examples with sample string for which match is failing. May 12, 2014 at 7:22
  • What about using java features instead of regex? May 12, 2014 at 7:26
  • @Duncan : yes thats the requirement May 12, 2014 at 7:36
  • @Prasobh.K Is it ok for strings to also be 1-29 characters long? Or are they always only 30 or 32? May 12, 2014 at 7:45

2 Answers 2

4

Try the following pattern. This matches 1-29 "normal" characters, or exactly 30 characters with your optional suffix.

//                  Matches 29 chars    Matches 30 chars plus suffix
//                        |                      |
//                ----------------------------------------------
//                |               ||                           |
String pattern = "([A-Z\\d]{1,29})|([A-Z\\d]{30}([A-Z]\\d){0,1})";
//                 ^^^^^^^^         ^^^^^^^^

The underlined parts (^^^^) should be adjusted to describe the set of characters you allow in the first 30 characters.

Note: I've used 0-9 as valid numbers, which is more normal. If you really need 1-9 you can adjust the code.

6
  • that suffix is only 1 char long, you won't match 32 chars.
    – RokL
    May 12, 2014 at 7:34
  • @UMad The suffix is two characters: [A-Z]\\d. Test it out :-) May 12, 2014 at 7:42
  • This help me however i need to alter pattern like String pattern = "([A-Z\\d]{1,29})|([A-Z\\d]{30}([A-Z]{0,1}\\d{0,1})"; since last two characters are optional May 12, 2014 at 8:00
  • @Prasobh.K My pattern does exactly what you asked for. You said the valid strings were either 30 or 32 length. The pattern you just posted would allow 31 characters. May 12, 2014 at 8:06
  • Not sure you need to separate cases for 29 characters and 30+. A single pattern [A-Z\\d]{1,30}([A-Z]\\d){0,1} will match the same strings. Also I tend to avoid using capturing groups when they are unneeded, I prefer to use (?:...) instead of (....).
    – RokL
    May 12, 2014 at 8:10
1

Your text is unclear. If 31st character exists is the 32nd character optional or required?

[A-Z1-9]{0,30}?(?:[A-Z]|[A-Z][1-9])?

This one allows 30 characters, 31 characters or 32 characters.

[A-Z1-9]{0,30}?(?:[A-Z][1-9])?

This one allows 30 characters or 32 characters (i.e. if 31st is present then 32nd is required).

1
  • 31 and 32 character are optional.If 32 is present then it should be a number and 31st should be alphabet and not wiseversa May 12, 2014 at 7:35

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.