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. 10 2744
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.
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
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.
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.
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
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
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
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
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
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 This discussion thread is closed Replies have been disabled for this discussion. Similar topics
3 posts
views
Thread by Vibha Tripathi |
last post: by
|
5 posts
views
Thread by Bradley Plett |
last post: by
|
18 posts
views
Thread by Q. John Chen |
last post: by
|
5 posts
views
Thread by Ryan |
last post: by
|
7 posts
views
Thread by norton |
last post: by
|
7 posts
views
Thread by Billa |
last post: by
|
6 posts
views
Thread by Ludwig |
last post: by
|
3 posts
views
Thread by Zach |
last post: by
|
25 posts
views
Thread by Mike |
last post: by
| | | | | | | | | | |