简体   繁体   English

C#中用于内存分配的正则表达式

[英]Regular Expression for memory allocation in C#

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: 我的目标是使用-Xmx-Xms获取用于在Java虚拟机中设置内存的正则表达式,因此它需要传递以下字符串:

-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 . 因此,需要做的是正则表达式传递的数字是1024的倍数,除非字母是gG。

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] : "");

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. 它也将匹配-128m (与M,K和k类似), -128m和2056,但不会更高。

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): 这是一个正则表达式,如果格式为-xL (x是任何数字,L是除g或G之外的任何字母),则它与输入字符串中的数字匹配:

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

You will have your number ina capture group (if any) 您将在一个捕获组中找到您的号码(如果有的话)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM