Regex

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello,
I have a Regex:
Regex regex = new Regex(@"22 22 22 22 22 22 22 22 22 22(?
<centervalue>.+)33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33
33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33");

Sometimes my data has 10 sets of "22"'s , sometimes 12, 14 ,ect.
I want the centervalue to start after ALL of the "22"'s but this is
not happening.
Once it see's the 10 sets it starts there, but I want it to start at
the end, no matter how many sets.
Any suggestions?
Thanks,
Mike
 
You want to specificy at least 10 repeats like this
Regex regex = new Regex(@"(22 ){10,}(?
<centervalue>.+)(33 ){minimum allowable number,}");

I think that will do it.
If you enclose something in curly braces and follow it by the following
strings, you will get the following number of matches (from C# in a Nutshell
edition something)
* Any number of matches (including zero)
+ At least 1 match
? Exactly 1 match
{n} Exactly n matches
{n,} At least n matches
{n,m} At least n matches, No more than m matches


Ethan
 
You want to specificy at least 10 repeats like this
      Regex regex = new Regex(@"(22 ){10,}(?
 <centervalue>.+)(33 ){minimum allowable number,}");

I think that will do it.
If you enclose something in curly braces and follow it by the following
strings, you will get the following number of matches (from C# in a Nutshell
edition something)
*                      Any number of matches (including zero)
+                      At least 1 match
?                      Exactly 1 match
{n}                   Exactly n matches
{n,}                 At least n matches
{n,m}              At least n matches, No more than m matches

Ethan






- Show quoted text -

I'm getting:
"parsing \"(22 ){10,}(? <centervalue>.+)(33 ){10,}\" - Unrecognized
grouping construct."

Mike
 
There is an extra space after your question mark and before the less than

should be
(22 ){10,}(?<centervalue>.+)(33 ){10,}
not
(22 ){10,}(? <centervalue>.+)(33 ){10,}


At least this takes care of the parsing error. I have not tested it for real
functionality.
Ethan
 
How about:

string str = "22 22 22 22 22 22 My Value 33 33 33 33 33 33 33";

int start = str.LastIndexOf(" 22") + 3;
int end = str.IndexOf(" 33);

// Now you have the start and end locations of the value you're looking for
and can use SubString to parse it out, or a RegEx if you need more complex
parsing logic for it.

HTH
 

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