Click here to Skip to main content
15,889,751 members
Home / Discussions / C#
   

C#

 
QuestionRegex and Split() usage Pin
JoeSchmoe0074-Aug-10 9:05
JoeSchmoe0074-Aug-10 9:05 
AnswerRe: Regex and Split() usage Pin
Ennis Ray Lynch, Jr.4-Aug-10 9:26
Ennis Ray Lynch, Jr.4-Aug-10 9:26 
GeneralRe: Regex and Split() usage Pin
JoeSchmoe0074-Aug-10 9:28
JoeSchmoe0074-Aug-10 9:28 
GeneralRe: Regex and Split() usage Pin
T M Gray4-Aug-10 9:54
T M Gray4-Aug-10 9:54 
AnswerRe: Regex and Split() usage Pin
T M Gray4-Aug-10 9:42
T M Gray4-Aug-10 9:42 
GeneralRe: Regex and Split() usage Pin
JoeSchmoe0074-Aug-10 9:50
JoeSchmoe0074-Aug-10 9:50 
AnswerRe: Regex and Split() usage Pin
Anurag Gandhi4-Aug-10 19:56
professionalAnurag Gandhi4-Aug-10 19:56 
AnswerRe: Regex and Split() usage Pin
AspDotNetDev5-Aug-10 11:04
protectorAspDotNetDev5-Aug-10 11:04 
You should use Regex.Matches instead of Regex.Split. For example, take this input string:
11112233

When you apply this regex:
(?<first>.{4})(?=.{4})|(?<=.{4})(?<middle>.{2})(?=.{2})|(?<=.{6})(?<last>.{2})

You will get three results:
111
22
33

Note the regex has three sections, each separated by a pipe (|), which means "or". The first part:
(?<first>.{4})(?=.{4})

Finds any 4 characters that are followed by 4 characters. The middle part:
(?<=.{4})(?<middle>.{2})(?=.{2})

Finds any 2 characters that have 4 characters before them and 2 characters after them. The last part:
(?<=.{6})(?<last>.{2})

Finds any 2 characters that have 6 characters before them.

So, for the first match, you pad the end. For the middle matches, you pad the beginning and end. And for the last match, you pad the beginning. Not sure why you'd want to do this, but that's how it would be done. Performance may or may not be an issue... you'll have to test it with large strings. Also, if you use the regular expression more than once, make sure you use the compiled version (you can indicate a regex is compiled in the Regex constructor). Also, if you really are going to used groups, you could simplify the regex quite a bit:
(?<first>.{4})(?<middle>.{2})(?<last>.{2})

Then you can just get the sections by their group name (one match will be produced, but many groups will be in that match). And if you want to ignore certain sections of the string, you can do that too:
(?<first>.{4})(?<middle>.{2})(?<ignore>.{2})(?<last>.{2})(?<ignore>.{2})

Try that with this input string:
111122334455

And if you really want to use Regex.Split for some reason, something like this might work for the example you gave:
(?<=.{4})(?=.{2})


GeneralRe: Regex and Split() usage Pin
JoeSchmoe0075-Aug-10 11:16
JoeSchmoe0075-Aug-10 11:16 
QuestionIllegal characters in path. Pin
Vimalsoft(Pty) Ltd4-Aug-10 3:57
professionalVimalsoft(Pty) Ltd4-Aug-10 3:57 
AnswerRe: Illegal characters in path. Pin
Richard MacCutchan4-Aug-10 6:22
mveRichard MacCutchan4-Aug-10 6:22 
GeneralRe: Illegal characters in path. Pin
Vimalsoft(Pty) Ltd4-Aug-10 8:00
professionalVimalsoft(Pty) Ltd4-Aug-10 8:00 
AnswerRe: Illegal characters in path. Pin
DaveyM694-Aug-10 11:58
professionalDaveyM694-Aug-10 11:58 
QuestionBinding Source Issue Pin
spankyleo1234-Aug-10 2:59
spankyleo1234-Aug-10 2:59 
AnswerRe: Binding Source Issue Pin
Ennis Ray Lynch, Jr.4-Aug-10 5:02
Ennis Ray Lynch, Jr.4-Aug-10 5:02 
GeneralRe: Binding Source Issue Pin
spankyleo1234-Aug-10 5:23
spankyleo1234-Aug-10 5:23 
QuestionC#.NET 3.5 Excel 2007 Ribbon Add-in VS 2008 run macro Pin
Wheels0124-Aug-10 2:56
Wheels0124-Aug-10 2:56 
AnswerRe: C#.NET 3.5 Excel 2007 Ribbon Add-in VS 2008 run macro Pin
Wheels0124-Aug-10 8:18
Wheels0124-Aug-10 8:18 
QuestionSQL Query string issue Pin
MumbleB4-Aug-10 2:54
MumbleB4-Aug-10 2:54 
AnswerRe: SQL Query string issue Pin
Eddy Vluggen4-Aug-10 3:02
professionalEddy Vluggen4-Aug-10 3:02 
AnswerRe: SQL Query string issue Pin
PIEBALDconsult4-Aug-10 3:10
mvePIEBALDconsult4-Aug-10 3:10 
GeneralRe: SQL Query string issue Pin
MumbleB4-Aug-10 4:35
MumbleB4-Aug-10 4:35 
GeneralRe: SQL Query string issue Pin
Eddy Vluggen4-Aug-10 10:12
professionalEddy Vluggen4-Aug-10 10:12 
GeneralRe: SQL Query string issue Pin
MumbleB4-Aug-10 11:27
MumbleB4-Aug-10 11:27 
GeneralRe: SQL Query string issue Pin
Eddy Vluggen5-Aug-10 0:26
professionalEddy Vluggen5-Aug-10 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.