473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

15
If I have a string as follows

XXXasdf23s5\r\n
asdflkoirfn329i4\r\n
sef29384ewrj28039\r\n
XXX123sd3t334\r\n
sdorfu23984rr\r\n
sdflk2893rjf\r\n
weirj2983jhwer2398\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)
Expand|Select|Wrap|Line Numbers
  1. 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);
  2.  
Thanks for any advice
Jan 24 '11 #1
2 7702
Sal Sal
15
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?
Jan 24 '11 #2
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:
Expand|Select|Wrap|Line Numbers
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.         }
  5.  
  6.         private void button1_Click(object sender, EventArgs e)
  7.         {
  8.             string original = "XXXasdf23s5\r\nasdflkoirfn329i4\r\nsef29384ewrj28039\r\nXXX123sd3t334\r\nsdorfu23984rr\r\nsdflk2893rjf\r\nweirj2983jhwer2398\r\nXXX12356789\r\n4w3ujr523q938er\r\n";
  9.  
  10.             // Generate list of items
  11.             List<string> items = extractData(original, "XXX");
  12.  
  13.             // After this line you are left with a list
  14.             // of all the items that were found, to do
  15.             // with as you wish
  16.  
  17.             // #----- Example -----#
  18.             // Generate Message Box with a list of the
  19.             // found items
  20.  
  21.             string message = "";
  22.             int itemNo = 0;
  23.  
  24.             // Display original string
  25.             MessageBox.Show("Orignal String:\r\n\r\n" + original);
  26.  
  27.             foreach (string item in items)
  28.             {
  29.                 message = message + "\r\nItem " + itemNo + ":\r\n" + item;
  30.                 itemNo++;
  31.             }
  32.             MessageBox.Show(message);
  33.             // #--- End Example ---#
  34.         }
  35.  
  36.         /// <summary>
  37.         /// This method will extract all items
  38.         /// starting with the tag provided.
  39.         /// </summary>
  40.         /// <param name="contents">This is the string that the items shall be extracted from.</param>
  41.         /// <param name="tag">This is the string that each item is 'tagged' with.</param>
  42.         /// <returns></returns>
  43.         private List<string> extractData(string contents, string tag)
  44.         {
  45.             string item = "";
  46.             int indexStart = 0;
  47.             int indexEnd = 0;
  48.             List<string> itemsList = new List<string>();
  49.  
  50.             // Will continue to loop until all items are found
  51.             while (indexStart != -1 && indexEnd != -1)
  52.             {
  53.                 // Find first occurance
  54.                 indexStart = contents.IndexOf(tag);
  55.  
  56.                 // If indexStart = -1, it didn't find a tag,
  57.                 // therefore do nothing.
  58.  
  59.                 if (indexStart != -1)
  60.                 {
  61.                     // i.e. found an occurance
  62.  
  63.                     // If tag is found, Trim contents before the tag
  64.                     contents = contents.Remove(0, indexStart + tag.Length);
  65.  
  66.                     // Find next tag
  67.                     indexEnd = contents.IndexOf(tag);
  68.  
  69.                     if (indexEnd != -1)
  70.                     {
  71.                         // i.e. found another tag
  72.  
  73.                         // Isolate item from contents
  74.                         item = contents.Substring(0, indexEnd - 1);
  75.  
  76.                         // Remove item from contents
  77.                         contents = contents.Remove(0, indexEnd - 1);
  78.  
  79.                         // Add item to list
  80.                         itemsList.Add(item);
  81.                     }
  82.                     else
  83.                     {
  84.                         // i.e. couldn't find another tag,
  85.                         // therefore it must be the last item
  86.  
  87.                         // Add the rest of contents to list
  88.                         itemsList.Add(contents);
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             return itemsList;
  94.         }
Jan 25 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". ...
0
by: Mohee | last post by:
In VB.NET I am trying to create a regular expression that will validate any string as long as it does not contain a specified string. For example, I want to match any word that does not contain...
2
by: GreggTB | last post by:
Hello, I'm trying to perform a very simple validation of user input. I want to verify that the user entered a six-digit string consisting entirely of numbers. So anything from 000000 to 999999 is...
4
by: Johnny Lee | last post by:
Hi, I've met a problem in match a regular expression in python. Hope any of you could help me. Here are the details: I have many tags like this: xxx<a href="http://xxx.xxx.xxx" xxx>xxx xxx<a...
17
by: Randy Webb | last post by:
I know that the /g flag will match all occurrences. Is there a way, with a Regular Expression, to match all occurrences *except* the last one? pattern = /df/g; var myString = "asdfasdfasdfasdf";...
2
by: teo | last post by:
match word with interpunctuation Hallo, I need to build a regular expression able to match a specified word, preceded and followed by few chars (mainly interpunctuation) Below the code. ...
2
by: Kai Rosenthal | last post by:
Hello, how can I resolve envionment variables in a string. e.g. strVar = /myVar resolve in str1 = /mytest02/$MYVAR/mytest02 --/mytest02//myVar/mytest02 (unix) str2 =$MYVAR/mytest03 ...
1
by: teo | last post by:
Hallo, I'd like to match a given string, let's say fghilm in a bigger string, let's say abcdefghilmnopq but I'd like to have also a toleranche, let's say about 1 chars
1
by: sonia93 | last post by:
Hi , I am having a variable which has "++" or "+" as part of it . My file consists something like this air++ water fire+ I want to go one by one to each line and match a particular word...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.