469,903 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

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 2907
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by engwar1 | last post: by
2 posts views Thread by Tim Conner | last post: by
6 posts views Thread by Du Dang | last post: by
17 posts views Thread by clintonG | last post: by
5 posts views Thread by Chris | last post: by
6 posts views Thread by Martin Evans | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
6 posts views Thread by | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.