Note that there are some explanatory texts on larger screens.

plurals
  1. POA More Efficient Way to Parse a String in C#
    primarykey
    data
    text
    <p>I have this code that reads a file and creates Regex groups. Then I walk through the groups and use other matches on keywords to extract what I need. I need the stuff between each keyword and the next space or newline. I am wondering if there is a way using the Regex keyword match itself to discard what I don't want (the keyword). </p> <pre><code>//create the pattern for the regex String VSANMatchString = @"vsan\s(?&lt;number&gt;\d+)[:\s](?&lt;info&gt;.+)\n(\s+name:(?&lt;name&gt;.+)\s+state:(?&lt;state&gt;.+)\s+\n\s+interoperability mode:(?&lt;mode&gt;.+)\s\n\s+loadbalancing:(?&lt;loadbal&gt;.+)\s\n\s+operational state:(?&lt;opstate&gt;.+)\s\n)?"; //set up the patch MatchCollection VSANInfoList = Regex.Matches(block, VSANMatchString); // set up the keyword matches Regex VSANNum = new Regex(@" \d* "); Regex VSANName = new Regex(@"name:\S*"); Regex VSANState = new Regex(@"operational state\S*"); //now we can extract what we need since we know all the VSAN info will be matched to the correct VSAN //match each keyword (name, state, etc), then split and extract the value foreach (Match m in VSANInfoList) { string num=String.Empty; string name=String.Empty; string state=String.Empty; string s = m.ToString(); if (VSANNum.IsMatch(s)) { num=VSANNum.Match(s).ToString().Trim(); } if (VSANName.IsMatch(s)) { string totrim = VSANName.Match(s).ToString().Trim(); string[] strsplit = Regex.Split (totrim, "name:"); name=strsplit[1].Trim(); } if (VSANState.IsMatch(s)) { string totrim = VSANState.Match(s).ToString().Trim(); string[] strsplit=Regex.Split (totrim, "state:"); state=strsplit[1].Trim(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload