简体   繁体   English

如何使用 Regex.split 显示指定的字符串?

[英]How can I use Regex.split to display a specified string?

string cmd = "Static hostname: gatewayIcon name: computer - desktopChassis: desktopMachine ID: 706ecaf18cea4c919eb515d0e73c7fe4Boot ID: c2f95d12ab334e57ac7e6503f7d0cd87Operating System: CentOS Linux 8(Core)CPE OS Name: cpe:/ o:centos: centos: 8Kernel: Linux 4.18.0 - 147.3.1.el8_1.x86_64Architecture: x86 - 64Thu Dec 30 15:31:24 EST 2021total used free shared buff / cache availableMem: 7887756 455084 4067136 427084 3365536 6746868Swap: 8183804 0 8183804Filesystem Size Used Avail Use % Mounted ondevtmpfs 3.8G 0 3.8G 0 % / devtmpfs 3.8G 84K 3.8G 1 % / dev / shmtmpfs 3.8G 417M 3.4G 11 % / runtmpfs 3.8G 0 3.8G 0 % / sys / fs / cgroup/ dev / mapper / cl - root 50G 3.2G 47G 7 % // dev / sda2 976M 188M 721M 21 % / boot/ dev / sda1 599M 6.8M 593M 2 % / boot / efi/ dev / mapper / cl - home 53G 12G 42G 22 % / hometmpfs 771M 200K 771M 1 % / run / user / 1000total 120G 15G 105G 13 % -procs---------- - memory------------ - swap------ - io---- - system--------cpu---- -r b swpd free buff cache si so bi bo in cs us sy id wa st2 0 0 4067108 4884 3360652 0 0 0 2 1 3 37 2 61 0 0";
diagtext = cmd;

split = Regex.Split(diagtext, @"%");

foreach (var item in split)
{
   diskmem = split[10];
}

So I have this long string called cmd and I'm using regex to display a particular string next to the percentage % (sign).所以我有一个名为cmd的长字符串,我正在使用正则表达式在% (符号)旁边显示一个特定的字符串。 I was able to display this string: / run / user / 1000total 120G 15G 105G 13 % , but I only need the 13 not the strings before 13 .我能够显示这个字符串: / run / user / 1000total 120G 15G 105G 13 % ,但我只需要13而不是13之前的字符串。

How can I do this?我怎样才能做到这一点?

Make another split using a white space delimiter for each index of the split[] array, the last index of the resplit[] array will be the string before the % sign until the first white space occurrence:split[]数组的每个索引使用空格分隔符进行另一个拆分, resplit[]数组的最后一个索引将是 % 符号之前的字符串,直到第一个空格出现:

static void SomeMethod()
{
    string cmd = @"Static hostname: gatewayIcon name: computer - desktopChassis: desktopMachine ID:
                   706ecaf18cea4c919eb515d0e73c7fe4Boot ID: c2f95d12ab334e57ac7e6503f7d0cd87Operating System:
                   CentOS Linux 8(Core)CPE OS Name: cpe:/ o:centos: centos: 8Kernel: Linux 4.18.0 - 147.3.1.el8_1.x86_64Architecture: x86
                   - 64Thu Dec 30 15:31:24 EST 2021total used free shared buff / cache availableMem:
                   7887756 455084 4067136 427084 3365536 6746868Swap: 8183804 0 8183804Filesystem Size
                   Used Avail Use % Mounted ondevtmpfs
                   3.8G 0 3.8G 0 % / devtmpfs 3.8G 84K 3.8G 1 % / dev / shmtmpfs 3.8G 417M 3.4G 11 % / runtmpfs
                   3.8G 0 3.8G 0 % / sys / fs / cgroup/ dev / mapper / cl
                   - root 50G 3.2G 47G 7 % // dev / sda2 976M 188M 721M 21
                   % / boot/ dev / sda1 599M 6.8M 593M 2 % / boot / efi/ dev
                   / mapper / cl - home 53G 12G 42G 22 % / hometmpfs 771M 200K
                   771M 1 % / run / user / 1000total 120G 15G 105G 13
                   % -procs---------- - memory------------ - swap------ - io---- - system--------cpu---- -r b
                   swpd free buff cache si so bi bo in cs us sy id wa st2 0 0 4067108 4884 3360652 0 0 0 2 1 3 37 2 61 0 0";
    diagtext = cmd;    
    split = Regex.Split(diagtext, @"%");    
    var resplit = split[10].Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    // OUTPUT: '13'
    string finalResult = resplit[resplit.Length - 1];        
}

You're missing some pattern we can use to get a consistent result, but assuming the string you parse is the penultimate as per the sample string you show, you can use Split to get the last value of the penultimate split string:您缺少一些我们可以用来获得一致结果的模式,但假设您解析的字符串是您显示的示例字符串的倒数第二个,您可以使用Split来获取倒数第二个split字符串的最后一个值:

//...
var split = Regex.Split(diagtext, @"%"); // your code

var target = split.ElementAt(split.Length - 2).Split(); //split the string
var val = target.ElementAt(target.Length - 2); //isolate the value 13

Live demo现场演示

Or或者

var target = split.ElementAt(split.Length - 2).Split()
    .Where(p => p != string.Empty).ToArray();
//...

If you want to skip spaces.如果你想跳过空格。

I am not aloud to comment, so I'll put my comment in this answer.我没有大声发表评论,所以我会在这个答案中发表我的评论。 A little more information about the data structure is needed to properly answer this question, but at first glance, it looks like you can split this string with spaces and grab the 10th item.要正确回答这个问题,需要更多关于数据结构的信息,但乍一看,您似乎可以用空格分割这个字符串并获取第 10 项。 On another note, I would like to know why your are using a foreach loop.另一方面,我想知道您为什么使用 foreach 循环。 You are not using var item.您没有使用 var 项。

string[] items = diskmem.Split(' ');

You can use this regex string to get all percentage counts before any % symbols:您可以使用此正则表达式字符串在任何 % 符号之前获取所有百分比计数:

([\d]{1,3})\s%

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

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