Regular expression question

L

Lee Kuhn

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

mikeb

Lee said:
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.
 
B

Bruno Jouhier [MVP]

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

Bruno
 
L

Lee Kuhn

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
 
M

Michael Voss

Hi!

Lee Kuhn said:
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
 
L

Lee Kuhn

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 said:
Hi!

Lee Kuhn said:
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
 
M

mikeb

Lee said:
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?

Hi!

Lee Kuhn said:
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
 
L

Lee Kuhn

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


mikeb said:
Lee said:
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?

Hi!

:

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
 
M

mikeb

Lee said:
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.
Lee said:
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?



Hi!

:


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
 
L

Lee Kuhn

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 said:
Lee said:
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.
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?



Hi!

:


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
 

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