|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help with regular expressioninput = "1.2.3.4" pattern = @"\d+\.\d+\.\d+\.\d+" But the problem is, three of them are optional and it is important which ones get matched. The numbers need to be matched like this: c c.d a.c.d a.b.c.d I can get close with this pattern: pattern = @"(((?<a>\d+)\.)?(?<b>\d+)\.)?(?<c>\d+)(\.(?<d>\d+))?" But I can't seem to figure out how to make 'd' a higher priority than 'a' and 'b, because with "2.3.4" it is matching a.b.c instead of a.c.d. Any input would be appreciated... Seems to me this pattern should do the trick:
input = "1.2.3.4" pattern = @"\A((?<a>\d+)\.((?<b>\d+)\.)??)??(?<c>\d+)(\.(?<d>\d+))?\Z"; And it does in most cases, but for some reason when all four numbers are specified, it matches this: a = "2" b = "" c = "3" d = "4" Looking through all the captures, it doesn't even match "1" at all. How can this regular expression skip over "1"? I have the \A and \Z there, so if all items weren't captured, it should fail, no? Show quote "Bob" <nob***@nowhere.com> wrote in message news:uBWZV4AvGHA.3500@TK2MSFTNGP06.phx.gbl... >I have numbers I am trying to match, for example: > > input = "1.2.3.4" > pattern = @"\d+\.\d+\.\d+\.\d+" > > But the problem is, three of them are optional and it is important which > ones get matched. The numbers need to be matched like this: > > c > c.d > a.c.d > a.b.c.d > > I can get close with this pattern: > > pattern = @"(((?<a>\d+)\.)?(?<b>\d+)\.)?(?<c>\d+)(\.(?<d>\d+))?" > > But I can't seem to figure out how to make 'd' a higher priority than 'a' > and 'b, because with "2.3.4" it is matching a.b.c instead of a.c.d. > > Any input would be appreciated... > > > > This is the only way I have been able to get this to work:
const String pattern = @"\A(((?<a>\d+)\.(?<b>\d+)\.(?<c>\d+)\.(?<d>\d+))|" + @"((?<a>\d+)\.(?<c>\d+)\.(?<d>\d+))|" + @"((?<c>\d+)\.(?<d>\d+))|" + @"((?<c>\d+)))\Z"; If anybody has a better way, I would be interested in hearing of it. Show quote "Bob" <nob***@nowhere.com> wrote in message news:ete$zMBvGHA.4612@TK2MSFTNGP02.phx.gbl... > Seems to me this pattern should do the trick: > > input = "1.2.3.4" > pattern = @"\A((?<a>\d+)\.((?<b>\d+)\.)??)??(?<c>\d+)(\.(?<d>\d+))?\Z"; > > And it does in most cases, but for some reason when all four numbers are > specified, it matches this: > > a = "2" > b = "" > c = "3" > d = "4" > > Looking through all the captures, it doesn't even match "1" at all. How > can this regular expression skip over "1"? I have the \A and \Z there, so > if all items weren't captured, it should fail, no? > > > "Bob" <nob***@nowhere.com> wrote in message > news:uBWZV4AvGHA.3500@TK2MSFTNGP06.phx.gbl... >>I have numbers I am trying to match, for example: >> >> input = "1.2.3.4" >> pattern = @"\d+\.\d+\.\d+\.\d+" >> >> But the problem is, three of them are optional and it is important which >> ones get matched. The numbers need to be matched like this: >> >> c >> c.d >> a.c.d >> a.b.c.d >> >> I can get close with this pattern: >> >> pattern = @"(((?<a>\d+)\.)?(?<b>\d+)\.)?(?<c>\d+)(\.(?<d>\d+))?" >> >> But I can't seem to figure out how to make 'd' a higher priority than 'a' >> and 'b, because with "2.3.4" it is matching a.b.c instead of a.c.d. >> >> Any input would be appreciated... >> >> >> >> > > You're going to have t define your rules better than that. You cannot define
a rule set by an example. The example you gave shows 4 numbers delimited by periods, but it doesn't state whether the periods are decimal places, whether the decimal places are between digits in the same numbers, or, perhaps, whether the numbers will always be delimited by single periods, doubled periods, or perhaps by any other character. In addition, you use variables (a,b,c,d) to refer to something about some rule, but do not associate them specifically. And your regular expression seems to imply that there will always be four groups of 1 or more digits, delimited by single periods, but you have not stated this. A regular expression, like any programming solution, is an expression of a process that defines and follows a set of logical rules. To solve the problem, the problem must be fully defined to begin with. In my experience, defining the problem fully often points directly to the solution. -- Show quoteHTH, Kevin Spencer Microsoft MVP Chicken Salad Surgery Accept the Unexpected. "Bob" <nob***@nowhere.com> wrote in message news:uBWZV4AvGHA.3500@TK2MSFTNGP06.phx.gbl... >I have numbers I am trying to match, for example: > > input = "1.2.3.4" > pattern = @"\d+\.\d+\.\d+\.\d+" > > But the problem is, three of them are optional and it is important which > ones get matched. The numbers need to be matched like this: > > c > c.d > a.c.d > a.b.c.d > > I can get close with this pattern: > > pattern = @"(((?<a>\d+)\.)?(?<b>\d+)\.)?(?<c>\d+)(\.(?<d>\d+))?" > > But I can't seem to figure out how to make 'd' a higher priority than 'a' > and 'b, because with "2.3.4" it is matching a.b.c instead of a.c.d. > > Any input would be appreciated... > > > > |
|||||||||||||||||||||||