Connecting Tech Pros Worldwide Help | Site Map

Regular expression question

Lee Kuhn
Guest
 
Posts: n/a
#1: Nov 16 '05
I am trying the create a regular expression that will essentially match
characters in the middle of a fixed-length string. The string may be any
characters, but will always be the same length. In other words, as the
regular expression (....)($) matches the "4567" in the string "1234567", how
would I create a similar regular expression that only matches the "45" in
the same string. The same regular expression would match "32" in the string
"00032999".

Any help is greatly appreciated.


Bruno Jouhier [MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Regular expression question


Did you try (..)..$

Bruno.

"Lee Kuhn" <lee_kuhn@grsinc.com> a écrit dans le message de
news:eXEnsvXKEHA.2624@TK2MSFTNGP09.phx.gbl...[color=blue]
> I am trying the create a regular expression that will essentially match
> characters in the middle of a fixed-length string. The string may be any
> characters, but will always be the same length. In other words, as the
> regular expression (....)($) matches the "4567" in the string "1234567",[/color]
how[color=blue]
> would I create a similar regular expression that only matches the "45" in
> the same string. The same regular expression would match "32" in the[/color]
string[color=blue]
> "00032999".
>
> Any help is greatly appreciated.
>
>[/color]


mikeb
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Regular expression question


Lee Kuhn wrote:[color=blue]
> I am trying the create a regular expression that will essentially match
> characters in the middle of a fixed-length string. The string may be any
> characters, but will always be the same length. In other words, as the
> regular expression (....)($) matches the "4567" in the string "1234567", how
> would I create a similar regular expression that only matches the "45" in
> the same string. The same regular expression would match "32" in the string
> "00032999".
>[/color]

Maybe I'm misunderstanding something, but it seems String.Substring()
would be the thing to use for this.

Using a regex seems like overkill.

--
mikeb
Bruno Jouhier [MVP]
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Regular expression question


I was wrong, you need curly braces instead of parentheses: {..}..$
But substring should do the job. Why use regexp?

Bruno

"Bruno Jouhier [MVP]" <bjouhier@club-internet.fr> a écrit dans le message de
news:eLfoA9XKEHA.4032@TK2MSFTNGP10.phx.gbl...[color=blue]
> Did you try (..)..$
>
> Bruno.
>
> "Lee Kuhn" <lee_kuhn@grsinc.com> a écrit dans le message de
> news:eXEnsvXKEHA.2624@TK2MSFTNGP09.phx.gbl...[color=green]
> > I am trying the create a regular expression that will essentially match
> > characters in the middle of a fixed-length string. The string may be any
> > characters, but will always be the same length. In other words, as the
> > regular expression (....)($) matches the "4567" in the string "1234567",[/color]
> how[color=green]
> > would I create a similar regular expression that only matches the "45"[/color][/color]
in[color=blue][color=green]
> > the same string. The same regular expression would match "32" in the[/color]
> string[color=green]
> > "00032999".
> >
> > Any help is greatly appreciated.
> >
> >[/color]
>
>[/color]


Lee Kuhn
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Regular expression question


The parentheses seem to work better than the curly braces. However, when I
use (..)..$, I end up with two groups: one is "4567" and the other is "45".
The "match" is still "4567". I need the match to be "45". The reason I am
using a regular expression for this is because I am trying to operate within
the confines of existing code, only modifying the actual regular expression.

Lee

"Bruno Jouhier [MVP]" <bjouhier@club-internet.fr> wrote in message
news:%23FnSxidKEHA.644@tk2msftngp13.phx.gbl...[color=blue]
> I was wrong, you need curly braces instead of parentheses: {..}..$
> But substring should do the job. Why use regexp?
>
> Bruno
>
> "Bruno Jouhier [MVP]" <bjouhier@club-internet.fr> a écrit dans le message[/color]
de[color=blue]
> news:eLfoA9XKEHA.4032@TK2MSFTNGP10.phx.gbl...[color=green]
> > Did you try (..)..$
> >
> > Bruno.
> >
> > "Lee Kuhn" <lee_kuhn@grsinc.com> a écrit dans le message de
> > news:eXEnsvXKEHA.2624@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > I am trying the create a regular expression that will essentially[/color][/color][/color]
match[color=blue][color=green][color=darkred]
> > > characters in the middle of a fixed-length string. The string may be[/color][/color][/color]
any[color=blue][color=green][color=darkred]
> > > characters, but will always be the same length. In other words, as the
> > > regular expression (....)($) matches the "4567" in the string[/color][/color][/color]
"1234567",[color=blue][color=green]
> > how[color=darkred]
> > > would I create a similar regular expression that only matches the "45"[/color][/color]
> in[color=green][color=darkred]
> > > the same string. The same regular expression would match "32" in the[/color]
> > string[color=darkred]
> > > "00032999".
> > >
> > > Any help is greatly appreciated.
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Michael Voss
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Regular expression question


Hi!

"Lee Kuhn" wrote:[color=blue]
> The parentheses seem to work better than the curly braces. However, when I
> use (..)..$, I end up with two groups: one is "4567" and the other is[/color]
"45".[color=blue]
> The "match" is still "4567". I need the match to be "45".[/color]
[...snip...]

(?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could find
"45" in Match.Groups["myGroup"].Value


Lee Kuhn
Guest
 
Posts: n/a
#7: Nov 16 '05

re: Regular expression question


I can definitely see how I could do what I want with groups (and named
groups). At this point, I am trying to determine if I can use a regular
expression to return a substring from the middle of a number (always the
same length) with Regex.Match.Value. The substring will always be from the
same position within the number.

Any thoughts?

"Michael Voss" <michael.voss@lvrREMOVE.deCAPS> wrote in message
news:408cff02$1@news...[color=blue]
> Hi!
>
> "Lee Kuhn" wrote:[color=green]
> > The parentheses seem to work better than the curly braces. However, when[/color][/color]
I[color=blue][color=green]
> > use (..)..$, I end up with two groups: one is "4567" and the other is[/color]
> "45".[color=green]
> > The "match" is still "4567". I need the match to be "45".[/color]
> [...snip...]
>
> (?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could find
> "45" in Match.Groups["myGroup"].Value
>
>[/color]


mikeb
Guest
 
Posts: n/a
#8: Nov 16 '05

re: Regular expression question


Lee Kuhn wrote:[color=blue]
> I can definitely see how I could do what I want with groups (and named
> groups). At this point, I am trying to determine if I can use a regular
> expression to return a substring from the middle of a number (always the
> same length) with Regex.Match.Value. The substring will always be from the
> same position within the number.1[/color]

Again, why not just use String.Substring()?
[color=blue]
>
> Any thoughts?
>
> "Michael Voss" <michael.voss@lvrREMOVE.deCAPS> wrote in message
> news:408cff02$1@news...
>[color=green]
>>Hi!
>>
>>"Lee Kuhn" wrote:
>>[color=darkred]
>>>The parentheses seem to work better than the curly braces. However, when[/color][/color]
>
> I
>[color=green][color=darkred]
>>>use (..)..$, I end up with two groups: one is "4567" and the other is[/color]
>>
>>"45".
>>[color=darkred]
>>>The "match" is still "4567". I need the match to be "45".[/color]
>>
>>[...snip...]
>>
>>(?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could find
>>"45" in Match.Groups["myGroup"].Value
>>
>>[/color]
>
>
>[/color]


--
mikeb
Lee Kuhn
Guest
 
Posts: n/a
#9: Nov 16 '05

re: Regular expression question


Because the code is already written and deployed. The regular expression is
exposed to me as an option.


"mikeb" <mailbox.google@nospam.mailnull.com> wrote in message
news:OElpiu6KEHA.2488@TK2MSFTNGP10.phx.gbl...[color=blue]
> Lee Kuhn wrote:[color=green]
> > I can definitely see how I could do what I want with groups (and named
> > groups). At this point, I am trying to determine if I can use a regular
> > expression to return a substring from the middle of a number (always the
> > same length) with Regex.Match.Value. The substring will always be from[/color][/color]
the[color=blue][color=green]
> > same position within the number.1[/color]
>
> Again, why not just use String.Substring()?
>[color=green]
> >
> > Any thoughts?
> >
> > "Michael Voss" <michael.voss@lvrREMOVE.deCAPS> wrote in message
> > news:408cff02$1@news...
> >[color=darkred]
> >>Hi!
> >>
> >>"Lee Kuhn" wrote:
> >>
> >>>The parentheses seem to work better than the curly braces. However,[/color][/color][/color]
when[color=blue][color=green]
> >
> > I
> >[color=darkred]
> >>>use (..)..$, I end up with two groups: one is "4567" and the other is
> >>
> >>"45".
> >>
> >>>The "match" is still "4567". I need the match to be "45".
> >>
> >>[...snip...]
> >>
> >>(?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could find
> >>"45" in Match.Groups["myGroup"].Value
> >>
> >>[/color]
> >
> >
> >[/color]
>
>
> --
> mikeb[/color]


mikeb
Guest
 
Posts: n/a
#10: Nov 16 '05

re: Regular expression question


Lee Kuhn wrote:[color=blue]
> Because the code is already written and deployed. The regular expression is
> exposed to me as an option.
>[/color]

I see. Then I think a regular expression such as:

(?:(?<=^.{2}))(.{4})

would do what you want - for the particular case where you want the
substring to start at index 2 and have a length of 4.

Here's a small method that'll build the appropriate regex expression
when passed in the index and length you want:

public static string SubstringRegex( int start, int len) {
return( String.Format("(?:(?<=^.{{{0}}}))(.{{{1}}})",
start, len));
}

Basically, the first part of the regex is a zero-lenth positive
lookbehind assertion wrapped in a non-capturing group. This matches the
characters at the beginning of the string that you want discarded.

The next bit of the regex is a group that captures the number of
characters you want in your substring.
[color=blue]
>
> "mikeb" <mailbox.google@nospam.mailnull.com> wrote in message
> news:OElpiu6KEHA.2488@TK2MSFTNGP10.phx.gbl...
>[color=green]
>>Lee Kuhn wrote:
>>[color=darkred]
>>>I can definitely see how I could do what I want with groups (and named
>>>groups). At this point, I am trying to determine if I can use a regular
>>>expression to return a substring from the middle of a number (always the
>>>same length) with Regex.Match.Value. The substring will always be from[/color][/color]
>
> the
>[color=green][color=darkred]
>>>same position within the number.1[/color]
>>
>>Again, why not just use String.Substring()?
>>
>>[color=darkred]
>>>Any thoughts?
>>>
>>>"Michael Voss" <michael.voss@lvrREMOVE.deCAPS> wrote in message
>>>news:408cff02$1@news...
>>>
>>>
>>>>Hi!
>>>>
>>>>"Lee Kuhn" wrote:
>>>>
>>>>
>>>>>The parentheses seem to work better than the curly braces. However,[/color][/color]
>
> when
>[color=green][color=darkred]
>>>I
>>>
>>>
>>>>>use (..)..$, I end up with two groups: one is "4567" and the other is
>>>>
>>>>"45".
>>>>
>>>>
>>>>>The "match" is still "4567". I need the match to be "45".
>>>>
>>>>[...snip...]
>>>>
>>>>(?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could find
>>>>"45" in Match.Groups["myGroup"].Value
>>>>
>>>>
>>>
>>>
>>>[/color]
>>
>>--
>>mikeb[/color]
>
>
>[/color]


--
mikeb
Lee Kuhn
Guest
 
Posts: n/a
#11: Nov 16 '05

re: Regular expression question


Unbelievable...I think that does exactly what I need. I don't how you came
up with that one. Thanks a lot for your help.

"mikeb" <mailbox.google@nospam.mailnull.com> wrote in message
news:OXuTln9KEHA.1192@TK2MSFTNGP11.phx.gbl...[color=blue]
> Lee Kuhn wrote:[color=green]
> > Because the code is already written and deployed. The regular expression[/color][/color]
is[color=blue][color=green]
> > exposed to me as an option.
> >[/color]
>
> I see. Then I think a regular expression such as:
>
> (?:(?<=^.{2}))(.{4})
>
> would do what you want - for the particular case where you want the
> substring to start at index 2 and have a length of 4.
>
> Here's a small method that'll build the appropriate regex expression
> when passed in the index and length you want:
>
> public static string SubstringRegex( int start, int len) {
> return( String.Format("(?:(?<=^.{{{0}}}))(.{{{1}}})",
> start, len));
> }
>
> Basically, the first part of the regex is a zero-lenth positive
> lookbehind assertion wrapped in a non-capturing group. This matches the
> characters at the beginning of the string that you want discarded.
>
> The next bit of the regex is a group that captures the number of
> characters you want in your substring.
>[color=green]
> >
> > "mikeb" <mailbox.google@nospam.mailnull.com> wrote in message
> > news:OElpiu6KEHA.2488@TK2MSFTNGP10.phx.gbl...
> >[color=darkred]
> >>Lee Kuhn wrote:
> >>
> >>>I can definitely see how I could do what I want with groups (and named
> >>>groups). At this point, I am trying to determine if I can use a regular
> >>>expression to return a substring from the middle of a number (always[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> >>>same length) with Regex.Match.Value. The substring will always be from[/color]
> >
> > the
> >[color=darkred]
> >>>same position within the number.1
> >>
> >>Again, why not just use String.Substring()?
> >>
> >>
> >>>Any thoughts?
> >>>
> >>>"Michael Voss" <michael.voss@lvrREMOVE.deCAPS> wrote in message
> >>>news:408cff02$1@news...
> >>>
> >>>
> >>>>Hi!
> >>>>
> >>>>"Lee Kuhn" wrote:
> >>>>
> >>>>
> >>>>>The parentheses seem to work better than the curly braces. However,[/color]
> >
> > when
> >[color=darkred]
> >>>I
> >>>
> >>>
> >>>>>use (..)..$, I end up with two groups: one is "4567" and the other is
> >>>>
> >>>>"45".
> >>>>
> >>>>
> >>>>>The "match" is still "4567". I need the match to be "45".
> >>>>
> >>>>[...snip...]
> >>>>
> >>>>(?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could[/color][/color][/color]
find[color=blue][color=green][color=darkred]
> >>>>"45" in Match.Groups["myGroup"].Value
> >>>>
> >>>>
> >>>
> >>>
> >>>
> >>
> >>--
> >>mikeb[/color]
> >
> >
> >[/color]
>
>
> --
> mikeb[/color]


Closed Thread