1

I have a query like need to generatre a regex for the String like "12A15" or "12a15".here it has 3 componeners [1-9 max length will be 2][only d or D][1-9 lmax length will be 2] .

I have written Regex to validate a string but still its not able to validate give numbers:

(^[1-9]{1,2}[{d,D}][{4,6,8,10,12,20}]{1,2})+(\s?(\+|\-)\s?)([1-9]{1,2}([{d,D}][{4,6,8,10,12,20}]{1,2}))$

Here, in the regular expression, I am validating a string 12D22 +3d20. It's telling it is matched, but it should not because 22 is not there in the list. In a valid 5D20+5d12 string, 12 and 20 could be any number from the 4, 6, 8, 12, 20 list.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Akhil Patro
  • 306
  • 2
  • 13
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Jul 06 '19 at 14:17
  • Hi @ Wiktor Stribiżew , Thanks for the help.I have written Regex to validate a string but still its not able to validate give numbers ,(^[1-9]{1,2}[{d,D}][{4,6,8,10,12,20}]{1,2})+(\s?(\+|\-)\s?)([1-9]{1,2}([{d,D}][{4,6,8,10,12,20}]{1,2}))$" here in the regular expression am validating a string "12D22 +3d20" its telling as matched but it should not because 22 is not there in the list.Could you please help me on it. Thanks – Akhil Patro Jul 09 '19 at 02:12
  • 1
    From what you wrote in the comment, I suspect you want `^[0-9]{1,2}[dD]([468]|1[02]|20)\s?[+-]\s?[0-9]{1,2}[dD]([468]|1[02]|20)$`, [demo](https://regex101.com/r/VYLb8E/1). – Wiktor Stribiżew Jul 09 '19 at 15:07
  • I need a validation like in a string 5D20+5d12 here the character 12 and 20 could be anything in from the list(4,6,8,12,20) , but my Expression not able to validate if you use like 5d12+5d81 as 8 and 1 both are exists.but i need only the number which i have specified(4,6,8,12,20) only them – Akhil Patro Jul 11 '19 at 08:21
  • So, does [the expression above](https://stackoverflow.com/questions/56914911/how-to-generate-a-regex-code-for-string-like-this-12a13?noredirect=1#comment100449537_56914911) works for you? – Wiktor Stribiżew Jul 11 '19 at 08:23
  • No it does not work – Akhil Patro Jul 11 '19 at 08:26
  • Why? Look, [it works fine](https://regex101.com/r/VYLb8E/3). – Wiktor Stribiżew Jul 11 '19 at 08:28
  • @WiktorStribiżew thanks, it worked fine, some how i did not copy properly for which it was not working. – Akhil Patro Jul 11 '19 at 10:23
  • I posted the answer, please consider [accepting/upvoting](https://stackoverflow.com/help/someone-answers). – Wiktor Stribiżew Jul 11 '19 at 11:13

1 Answers1

1

You may use

^[0-9]{1,2}[dD]([468]|1[02]|20)\s?[+-]\s?[0-9]{1,2}[dD]([468]|1[02]|20)$

See the regex demo and the regex graph:

enter image description here

Details

  • ^ - start of string (omit if used in .matches())
  • [0-9]{1,2} - 1 or 2 digits
  • [dD] - d or D
  • ([468]|1[02]|20) - 4, 6, 8, 10, 12 or 20
  • \s? - an optional whitespace
  • [+-] - + or -
  • \s? - an optional whitespace
  • [0-9]{1,2}[dD]([468]|1[02]|20) - see above
  • $ - end of string (omit if used in .matches()).

In Java:

String block = "[0-9]{1,2}[dD](?:[468]|1[02]|20)";
String regex = block + "\\s?[+-]\\s?" + block;
bool matched = your_string.matches(regex);
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • Here in the line 2 (block + "\\s?[+-]\\s?" + block) its missing the "^" Start symbol and "$" from end , is it correct? – Akhil Patro Jul 13 '19 at 04:44
  • @AkhilPatro Absolutely correct. Please pay attention what I wrote: *`^` - start of string (omit if used in `.matches()`)... `$` - end of string (omit if used in `.matches()`)*. The `matches()` method requires a full string match. – Wiktor Stribiżew Jul 13 '19 at 17:17