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) - 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
2 7739
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?
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: - 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;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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.......
|
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
|
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.
| |
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.
------------------------
|
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)
|
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
|
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.
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |