Return Data Regex Doesn't Isolate - Yikes

  • Thread starter Thread starter Garibaldi
  • Start date Start date
G

Garibaldi

Folks,

I'm having a bad regex day and can sure use your help, please..

I have a Regex expression that works fine. It's purpose is to isolate all
data from the start of a string begining with 200~ to the end of the string
but before the start of the next 200~. Here's the regex expression and test
data:

(?ms)^200~(.*?)(?=^200~)

Here's some test data

00000000000000000000
200~11111111111111111
22222222222222222222222222222
3333333333333
200~444444444444444444444444
555555555555555555555555555555555
66666666
200~777777777777777777
888888888888

The regex will return 200~ plus all ones, twos, threes as a group then 200~
plus all fours, fives, sixes as a group

Problem
---------
Now I need to do the reverse. I need to return all the data the preceding
regex doesn't return. IOW, I need to return all the zeros and 200~ plus all
the sevens and all the eights as a group. I want to execute "NOT
(?ms)^200~(.*?)(?=^200~)"

Sure could use a pointer to the correct starting point or any suggestion
that will help me reach my goal.

Thanks in advance

ty_92648 AT hotmail.com
 
I had some rather large explanation of how to do it procedurally, but then I
thought,
"Justin, get off your arse and write some regular expression magic!". So here
you go.

using System;
using System.Text.RegularExpressions;

public class SuperRegex {
private static string stuff =
@"00000000000000000000
200~11111111111111111
22222222222222222222222222222
3333333333333
200~444444444444444444444444
555555555555555555555555555555555
66666666
200~777777777777777777
888888888888";

private static void Main(string[] args) {
Regex regex = new Regex("(?ms)(?:\\A|^200~)(.*?)(?=^200~|\\Z)");
MatchCollection matches = regex.Matches(stuff);
for(int i = 0; i < matches.Count; i++) {
Console.WriteLine();
Console.WriteLine(matches.Value);
Console.WriteLine();
}
}
}
 
You could just call Regex.Replace with the same expression and an empty
string as the replacement if you didn't need to access any of the match
properties (index, groups, etc.).

Brian Davis
http://www.knowdotnet.com
 
Got it, thanks, Justin!

Justin Rogers said:
I had some rather large explanation of how to do it procedurally, but then I
thought,
"Justin, get off your arse and write some regular expression magic!". So here
you go.

using System;
using System.Text.RegularExpressions;

public class SuperRegex {
private static string stuff =
@"00000000000000000000
200~11111111111111111
22222222222222222222222222222
3333333333333
200~444444444444444444444444
555555555555555555555555555555555
66666666
200~777777777777777777
888888888888";

private static void Main(string[] args) {
Regex regex = new Regex("(?ms)(?:\\A|^200~)(.*?)(?=^200~|\\Z)");
MatchCollection matches = regex.Matches(stuff);
for(int i = 0; i < matches.Count; i++) {
Console.WriteLine();
Console.WriteLine(matches.Value);
Console.WriteLine();
}
}
}


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Garibaldi said:
Folks,

I'm having a bad regex day and can sure use your help, please..

I have a Regex expression that works fine. It's purpose is to isolate all
data from the start of a string begining with 200~ to the end of the string
but before the start of the next 200~. Here's the regex expression and test
data:

(?ms)^200~(.*?)(?=^200~)

Here's some test data

00000000000000000000
200~11111111111111111
22222222222222222222222222222
3333333333333
200~444444444444444444444444
555555555555555555555555555555555
66666666
200~777777777777777777
888888888888

The regex will return 200~ plus all ones, twos, threes as a group then 200~
plus all fours, fives, sixes as a group

Problem
---------
Now I need to do the reverse. I need to return all the data the preceding
regex doesn't return. IOW, I need to return all the zeros and 200~ plus all
the sevens and all the eights as a group. I want to execute "NOT
(?ms)^200~(.*?)(?=^200~)"

Sure could use a pointer to the correct starting point or any suggestion
that will help me reach my goal.

Thanks in advance

ty_92648 AT hotmail.com
 

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