Regex question

  • Thread starter Thread starter AMP
  • Start date Start date
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
 
(?<=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.
 
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
 
AMP said:
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".
 
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/library/system.text.regularexpressions.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.
 
Thomas said:
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
 
Ebbe Kristensen said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top