Search code examples
.nettext-parsingstring-parsing

Parsing text with open close tags


I need to parse Windows text file and extract all data related to operations. Operations are separated with $OPERATION and $OPERATION_END. What I need to do is extract all text blocks for all operations. How can I do it effectively using regex or simple String methods. I would appreciate you provide small snippet.

$OPERS_LIST
//some general data

$OPERATION
//some text block
$OPERATION_END


$OPERS_LIST_END

Solution

  • to get all operations from the list:

    var input = @"$OPERS_LIST
    //some general data
    
    $OPERATION
    
    erfgergwerg
    ewrg//some text block
    
    $OPERATION_END
    
    $OPERATION
    //some text block
    $OPERATION_END
    
    
    $OPERATION
    //some text block
    $OPERATION_END
    
    
    $OPERS_LIST_END";
    foreach (Match match in Regex.Matches(input, @"(?s)\$OPERATION(?<op>.+?)\$OPERATION_END"))
    {
     var operation = match.Groups["op"].Value;
    
     // do something with operation...
    }