473,750 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

15 New Member
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)
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 7739
Sal Sal
15 New Member
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
Samuel Jones
48 New Member
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
18752
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". Therefore some matches are: XYZAHIJ ABC
0
1803
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 the following strings: "--" or "/*" My main purpose is to not allow string that have Oracle comments in them.
2
2627
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 considered valid. The problem that I'm having is getting the validation to work on the entire string. In other words, 000000 is okay but 000000000000 is also returning as a match. Here's a quick code block...I have something along these lines.......
4
2150
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 href="wap://xxx.xxx.xxx" xxx>xxx xxx<a href="http://xxx.xxx.xxx" xxx>xxx ..... And I want to find all the "http://xxx.xxx.xxx" out, so I do it
17
11669
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"; var newString = myString.replace(pattern,'gh'); alert(newString) Gives me: asghasghasghasgh as it should. What I want: asghasghasghasdf Where the last one is not replaced.
2
1931
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
5281
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 --/myVar/mytest03 (unix)
1
1579
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
1240
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 of that line.
0
8838
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9583
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9396
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9342
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6808
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4888
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.