By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

Regular expression question

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Did you try (..)..$

Bruno.

"Lee Kuhn" <le******@grsinc.com> a écrit dans le message de
news:eX**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 16 '05 #2

P: n/a
Lee Kuhn wrote:
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".


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
Nov 16 '05 #3

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

Bruno

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> a écrit dans le message de
news:eL**************@TK2MSFTNGP10.phx.gbl...
Did you try (..)..$

Bruno.

"Lee Kuhn" <le******@grsinc.com> a écrit dans le message de
news:eX**************@TK2MSFTNGP09.phx.gbl...
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.


Nov 16 '05 #4

P: n/a
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]" <bj******@club-internet.fr> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
I was wrong, you need curly braces instead of parentheses: {..}..$
But substring should do the job. Why use regexp?

Bruno

"Bruno Jouhier [MVP]" <bj******@club-internet.fr> a écrit dans le message de news:eL**************@TK2MSFTNGP10.phx.gbl...
Did you try (..)..$

Bruno.

"Lee Kuhn" <le******@grsinc.com> a écrit dans le message de
news:eX**************@TK2MSFTNGP09.phx.gbl...
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.



Nov 16 '05 #5

P: n/a
Hi!

"Lee Kuhn" wrote:
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".

[...snip...]

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

P: n/a
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" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:408cff02$1@news...
Hi!

"Lee Kuhn" wrote:
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".

[...snip...]

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

Nov 16 '05 #7

P: n/a
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 the
same length) with Regex.Match.Value. The substring will always be from the
same position within the number.1
Again, why not just use String.Substring()?

Any thoughts?

"Michael Voss" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:408cff02$1@news...
Hi!

"Lee Kuhn" wrote:
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".


[...snip...]

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


--
mikeb
Nov 16 '05 #8

P: n/a
Because the code is already written and deployed. The regular expression is
exposed to me as an option.
"mikeb" <ma************@nospam.mailnull.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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 the
same length) with Regex.Match.Value. The substring will always be from the same position within the number.1


Again, why not just use String.Substring()?

Any thoughts?

"Michael Voss" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:408cff02$1@news...
Hi!

"Lee Kuhn" wrote:

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".

[...snip...]

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


--
mikeb

Nov 16 '05 #9

P: n/a
Lee Kuhn wrote:
Because the code is already written and deployed. The regular expression is
exposed to me as an option.

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.

"mikeb" <ma************@nospam.mailnull.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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 the
same length) with Regex.Match.Value. The substring will always be from
the
same position within the number.1


Again, why not just use String.Substring()?

Any thoughts?

"Michael Voss" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:408cff02$1@news...
Hi!

"Lee Kuhn" wrote:
>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".

[...snip...]

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



--
mikeb


--
mikeb
Nov 16 '05 #10

P: n/a
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" <ma************@nospam.mailnull.com> wrote in message
news:OX**************@TK2MSFTNGP11.phx.gbl...
Lee Kuhn wrote:
Because the code is already written and deployed. The regular expression is exposed to me as an option.


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.

"mikeb" <ma************@nospam.mailnull.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
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 thesame length) with Regex.Match.Value. The substring will always be from


the
same position within the number.1

Again, why not just use String.Substring()?
Any thoughts?

"Michael Voss" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:408cff02$1@news...
>Hi!
>
>"Lee Kuhn" wrote:
>
>
>>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".
>
>[...snip...]
>
>(?<myGroup>.{2}).{2} would match the "4567" in "1234567", you could find>"45" in Match.Groups["myGroup"].Value
>
>


--
mikeb


--
mikeb

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.