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). 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?
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];
}
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.
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(' ');