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
Ethan Strauss - 02 Apr 2008 15:29 GMT
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
> Hello,
> I have a Regex:
[quoted text clipped - 10 lines]
> Thanks,
> Mike
AMP - 02 Apr 2008 15:46 GMT
On Apr 2, 10:29 am, Ethan Strauss
<EthanStra...@discussions.microsoft.com> wrote:
> You want to specificy at least 10 repeats like this
> Regex regex = new Regex(@"(22 ){10,}(?
[quoted text clipped - 29 lines]
>
> - Show quoted text -
I'm getting:
"parsing \"(22 ){10,}(? <centervalue>.+)(33 ){10,}\" - Unrecognized
grouping construct."
Mike
Ethan Strauss - 02 Apr 2008 16:01 GMT
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
> On Apr 2, 10:29 am, Ethan Strauss
> <EthanStra...@discussions.microsoft.com> wrote:
[quoted text clipped - 37 lines]
>
> Mike
KH - 02 Apr 2008 18:28 GMT
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
> Hello,
> I have a Regex:
[quoted text clipped - 10 lines]
> Thanks,
> Mike