Group parsing not working as expected, can you suggest a fix?

B

bitwxtadpl

Hi,

I have a simple parsing regular expression that is expecting data and a
delimiter. Axy where A is the data and xy is the delimiter When the
delimiter is xy it works as expected. But, when I add one more char
'z' to the delimiter data it stops working.

It this an issue with my regular expression or with the C# regular
expression parser?

Can you suggest a fix?

Regular Expression #1 Works
^(?<PointId0>[^x][^y]*)xy

Data
Axy

Result
Pointid0 = A

Regular Expression #2 Fails
^(?<PointId0>[^x][^y][^Z]*)xyz

Data
Axyz

Result
Pointid0 = nothing


Thanks,
 
V

vijai.kalyan

Pardon me if I am wrong ... I have worked little with C# and even less
with regex' in C# but I will try.
Regular Expression #1 Works
^(?<PointId0>[^x][^y]*)xy
Regular Expression #2 Fails
^(?<PointId0>[^x][^y][^Z]*)xyz

Also, this reads (to me) like this:

[^x] - Match a single char that's not a 'x'.
[^y] - Match a single char that's not a 'y'.
[^Z]* - Match zero or more chars as long as they are not 'Z'

Followed by the literal string "xyz".

So on an input such as Axyz,

it matches A, then stops at 'x'. Now since you have a * after the ^Z, I
guess that matches zero 'Z's and stops ... I think what happens with
capturing groups is that multiple matches can take place. For example,
the above, I think will match

Axy

and

Axyz

so resulting in two groups?

Maybe the following will work better?

(?<PointId0>.*?)(xyz)

This, I think, will match the minimal number of characters until it
hits a (xyz).

hth,

-vijai.
 

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

Top