Question

I need a regular expression for multiple numbers and only one alpha character. My goal is to get the regular expression for setting memory in a Java Virtual Machine using -Xmx and -Xms so it needs to pass the following strings:

-1024k
-512M
-8g
-2048m

So what needs to be done is that the regular expression pass numbers that are multiple of 1024 except if the letter is g or G.

This is the code i'm using now and is not even working. Will appreciate the help

(Regex.IsMatch(options[2], "\\d\\^[KkGgMm]$") ? options[2] : "");
Was it helpful?

Solution

You could perhaps try:

(Regex.IsMatch(options[2], @"^-\d+[KkGgMm]$") ? options[2] : "");

Or if you really want to use regex to also validate the number (not advised, and it's has got limits too) you'll get something a bit like this:

(Regex.IsMatch(options[2], @"^-(?:\d+[Gg]|(?:128|256|512|1024|2056)[MmKk])$") ? options[2] : "");

Which will also match -128m (and similar with M, K and k), 256, 512, 1024 and 2056, but not higher.

OTHER TIPS

I am not sure if I understand what you want to achieve. Here is a regex that matches the number from the input string if it is in a form -xL (x is any number and L is any letter except for g or G):

^\-(\d+)[^Gg]\b

You will have your number ina capture group (if any)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top