By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,755 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,755 IT Pros & Developers. It's quick & easy.

Regex question

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
(?<=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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.