How to exclude part of a string from a regular expression match?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sal Sal
    New Member
    • Nov 2010
    • 15

    #1

    How to exclude part of a string from a regular expression match?

    If I have a string as follows

    XXXasdf23s5\r\n
    asdflkoirfn329i 4\r\n
    sef29384ewrj280 39\r\n
    XXX123sd3t334\r \n
    sdorfu23984rr\r \n
    sdflk2893rjf\r\ n
    weirj2983jhwer2 398\r\n
    XXX12356789\r\n
    4w3ujr523q938er \r\n

    basically I am searching for sections that begin with XXX but I don't know what the end will be. All I know is that the next section will definitely start with 'XXX'

    Can I search for ^XXX.*\r\n(.*\r \n)*XXX

    but add something to the regular expression to backtrack to before the next XXX that I matched and ignore it? I want my match to exclude it.

    (I tried (?=XXX) but I could not get it to work)
    Code:
    System.Text.RegularExpressions.Regex xxxRegex = new System.Text.RegularExpressions.Regex(@"^XXX\|.*\r\n(.*\r\n)+(?=XXX)", System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.Compiled);
    Thanks for any advice
  • Sal Sal
    New Member
    • Nov 2010
    • 15

    #2
    I changed my regular expression to this and it worked

    ^XXX.*\r\n(.*\r \n)+(?=^XXX)

    However sometimes when the match contains multiple XXX sections, it matches all the way to the last one and then my match includes multiple sections. I only want it to match one, any suggestions?

    Comment

    • Samuel Jones
      New Member
      • Jan 2011
      • 48

      #3
      This is a complicated way of doing it, but in one of fully-fledged programs, it has worked without fail.

      This method of doing it will create a LIST of ALL matches, you can then use this list as you wish.

      I have added comments to try to explain the process i have used.

      Here is the code to do it:
      Code:
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  string original = "XXXasdf23s5\r\nasdflkoirfn329i4\r\nsef29384ewrj28039\r\nXXX123sd3t334\r\nsdorfu23984rr\r\nsdflk2893rjf\r\nweirj2983jhwer2398\r\nXXX12356789\r\n4w3ujr523q938er\r\n";
                  
                  // Generate list of items
                  List<string> items = extractData(original, "XXX");
      
                  // After this line you are left with a list
                  // of all the items that were found, to do
                  // with as you wish
      
                  // #----- Example -----#
                  // Generate Message Box with a list of the
                  // found items
      
                  string message = "";
                  int itemNo = 0;
      
                  // Display original string
                  MessageBox.Show("Orignal String:\r\n\r\n" + original);
      
                  foreach (string item in items)
                  {
                      message = message + "\r\nItem " + itemNo + ":\r\n" + item;
                      itemNo++;
                  }
                  MessageBox.Show(message);
                  // #--- End Example ---#
              }
              
              /// <summary>
              /// This method will extract all items
              /// starting with the tag provided.
              /// </summary>
              /// <param name="contents">This is the string that the items shall be extracted from.</param>
              /// <param name="tag">This is the string that each item is 'tagged' with.</param>
              /// <returns></returns>
              private List<string> extractData(string contents, string tag)
              {
                  string item = "";
                  int indexStart = 0;
                  int indexEnd = 0;
                  List<string> itemsList = new List<string>();
      
                  // Will continue to loop until all items are found
                  while (indexStart != -1 && indexEnd != -1)
                  {
                      // Find first occurance
                      indexStart = contents.IndexOf(tag);
      
                      // If indexStart = -1, it didn't find a tag,
                      // therefore do nothing.
      
                      if (indexStart != -1)
                      {
                          // i.e. found an occurance
      
                          // If tag is found, Trim contents before the tag
                          contents = contents.Remove(0, indexStart + tag.Length);
      
                          // Find next tag
                          indexEnd = contents.IndexOf(tag);
      
                          if (indexEnd != -1)
                          {
                              // i.e. found another tag
      
                              // Isolate item from contents
                              item = contents.Substring(0, indexEnd - 1);
      
                              // Remove item from contents
                              contents = contents.Remove(0, indexEnd - 1);
                              
                              // Add item to list
                              itemsList.Add(item);
                          }
                          else
                          {
                              // i.e. couldn't find another tag,
                              // therefore it must be the last item
      
                              // Add the rest of contents to list
                              itemsList.Add(contents);
                          }
                      }
                  }
      
                  return itemsList;
              }

      Comment

      Working...