473,408 Members | 1,841 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,408 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 2983
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.