0
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). 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.

How can I do this?

1
  • You learn about regex and parse what you want from the string instead of splitting it at %. Not sure what 13 you mean ('% / run / user / 1000total 120G 15G 105G 13 %' ?) and how it is significant. Youi could as well split up diskmem at spaces and take the last one of that - something like diskmem.Split(" ").Last() with using System.Linq should work - your foreach (var item in split) is superfluous btw. it just creates work without accomplishing anything different fiom the first time you do diskmem = split[10]; Commented Jan 13, 2022 at 15:14

4 Answers 4

3

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:

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];        
}
Sign up to request clarification or add additional context in comments.

Comments

2

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:

//...
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.

Comments

1

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. On another note, I would like to know why your are using a foreach loop. You are not using var item.

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

Comments

0

You can use this regex string to get all percentage counts before any % symbols:

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

3 Comments

so, I've already pinned point the string I needed which is "13" on [10]. I just wanted to get the 13 to be displayed not the strings before it. Thank you
To do that you can just apply the regex in my answer to the string you already extracted and use the first match, which will be "13" in this case.
However i would advise you to parse the whole initial string using regex instead of just splitting at "%", as it looks like the order of entries in the string could change or additional drives could appear in the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.