473,385 Members | 1,752 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,385 software developers and data experts.

Regex question

AMP
Hello,
I have a long string that I need to take a section out of and use.
The section I need always starts with "XX XX XX XX XX"
and always ends with "YY YY YY YY YY"
I want to throw away the rest and use whats between the 2 Marker
strings.
I am not sure how to do this with Regex.
Any help would be appreciated
Thanks,
Mike

Oct 9 '06 #1
7 3248
I believe you're looking for named groups.

your regular expression would look something like "XX XX XX XX
XX(?<data>.*?)YY YY YY YY YY"

Then enumerate the matches to find the value for the "data" group in
each one.

http://www.regular-expressions.info/named.html

AMP wrote:
Hello,
I have a long string that I need to take a section out of and use.
The section I need always starts with "XX XX XX XX XX"
and always ends with "YY YY YY YY YY"
I want to throw away the rest and use whats between the 2 Marker
strings.
I am not sure how to do this with Regex.
Any help would be appreciated
Thanks,
Mike
Oct 9 '06 #2
(?<=XX XX XX XX XX).*(?=YY YY YY YY YY)

Uses a positive Look-behind, and a positive Look-ahead.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"AMP" <am******@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...
Hello,
I have a long string that I need to take a section out of and use.
The section I need always starts with "XX XX XX XX XX"
and always ends with "YY YY YY YY YY"
I want to throw away the rest and use whats between the 2 Marker
strings.
I am not sure how to do this with Regex.
Any help would be appreciated
Thanks,
Mike

Oct 9 '06 #3
AMP
I am new at regex, How do I use this in a statement. I want to operate
on the string between the 2 Markers.

Thnaks
Mike
Kevin Spencer wrote:
(?<=XX XX XX XX XX).*(?=YY YY YY YY YY)

Uses a positive Look-behind, and a positive Look-ahead.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"AMP" <am******@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.com...
Hello,
I have a long string that I need to take a section out of and use.
The section I need always starts with "XX XX XX XX XX"
and always ends with "YY YY YY YY YY"
I want to throw away the rest and use whats between the 2 Marker
strings.
I am not sure how to do this with Regex.
Any help would be appreciated
Thanks,
Mike
Oct 9 '06 #4
AMP <am******@gmail.comwrote:
Hello,
I have a long string that I need to take a section out of and use.
The section I need always starts with "XX XX XX XX XX"
and always ends with "YY YY YY YY YY"
I want to throw away the rest and use whats between the 2 Marker
strings.
I am not sure how to do this with Regex.
Any help would be appreciated
A quick and dirty example for you:

Regex regex = new Regex(@"^XX XX XX XX XX(?<centervalue>.+)YY YY YY YY YY");
Match match = regex.Match("XX XX XX XX XXTest StringYY YY YY YY YY");
if (match.Success)
System.Console.WriteLine("Matched Text = {0}",
match.Groups[@"centervalue"].Value);

The output should be "Test String".

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Oct 10 '06 #5
Hi Mike,

The way that the regular expression I posted works is that look-arounds are
non-capturing patterns. They indicate that the pattern indicated must match,
but it is not part of the match. So, to translate my regular expression:

(?<=XX XX XX XX XX).*(?=YY YY YY YY YY)

A match is zero or more of any character. It must be preceded by "XX XX XX
XX XX XX" and it must be followed by "YY YY YY YY YY YY". So that, for
example, in the string:

XX XX XX XX XX123 ABCYY YY YY YY YY

The match will be "123 ABC"

In the string:

XX XX XX XX XX.....YY YY YY YY YY

The match will be "..."

In the string

XX XX XX XX XX123 ABC

There is no match (no "YY..." sequence following)

I'm not sure what the question "How do I use this in a statement" means. But
here is the authoritative Regex reference:

http://msdn2.microsoft.com/en-us/lib...ons.regex.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"AMP" <am******@gmail.comwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
>I am new at regex, How do I use this in a statement. I want to operate
on the string between the 2 Markers.

Thnaks
Mike
Kevin Spencer wrote:
>(?<=XX XX XX XX XX).*(?=YY YY YY YY YY)

Uses a positive Look-behind, and a positive Look-ahead.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Shooter
http://unclechutney.blogspot.com

A man, a plan, a canal, a palindrome that has.. oh, never mind.

"AMP" <am******@gmail.comwrote in message
news:11*********************@c28g2000cwb.googlegr oups.com...
Hello,
I have a long string that I need to take a section out of and use.
The section I need always starts with "XX XX XX XX XX"
and always ends with "YY YY YY YY YY"
I want to throw away the rest and use whats between the 2 Marker
strings.
I am not sure how to do this with Regex.
Any help would be appreciated
Thanks,
Mike

Oct 10 '06 #6
Thomas T. Veldhouse wrote:
A quick and dirty example for you:

Regex regex = new Regex(@"^XX XX XX XX XX(?<centervalue>.+)YY YY YY YY
YY");
Since the OP talks about "a section", this is probably better:

Regex regex = new Regex(@"^.*XX XX XX XX XX(?<centervalue>.+)YY YY YY YY
YY.*$");

Ebbe
Oct 10 '06 #7
Ebbe Kristensen <eb**@ekologic.dkwrote:
Thomas T. Veldhouse wrote:
>A quick and dirty example for you:

Regex regex = new Regex(@"^XX XX XX XX XX(?<centervalue>.+)YY YY YY YY
YY");

Since the OP talks about "a section", this is probably better:

Regex regex = new Regex(@"^.*XX XX XX XX XX(?<centervalue>.+)YY YY YY YY
YY.*$");
Agreed.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Oct 10 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Tim Conner | last post by:
Hi, Thanks to Peter, Chris and Steven who answered my previous answer about regex to split a string. Actually, it was as easy as create a regex with the pattern "/*-+()," and most of my string...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
6
by: Phil Barber | last post by:
I am using Regex to validate a file name. I have everything I need except I would like the dot(.) in the filename only to appear once. My question is it possible to allow one instance of character...
6
by: | last post by:
Hi all, Sorry for the lengthy post but as I learned I should post concise-and-complete code. So the code belows shows that the execution of ValidateAddress consumes a lot of time. In the test...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...
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.