473,244 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,244 software developers and data experts.

Regular expression question

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
10 2966
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vibha Tripathi | last post by:
Hi Folks, I put a Regular Expression question on this list a couple days ago. I would like to rephrase my question as below: In the Python re.sub(regex, replacement, subject)...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
5
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
3
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.