Break Apart String Using Index 
Author Message
 Break Apart String Using Index

The following pseudo-vb is what I need and I just can't make happen:

Dim strTemplate
Dim strText
Dim strValue

strTemplate = "L %s - %s: ""%s<%s><%s>"" connected, address ""%s:%s"""
strText = "L 01/01/2003 - 01:30:02: ""MyUserName<123><123456><>"" connected,
address ""127.0.0.1:27015"""

strValue = BreakString(strTemplate, strText, 1)

Ok, the above quick code that I need and can't figure out is very cumbersome
for me.  It's not that I can't do it, it is just that it would take me too
long to do it and I was wondering if someone has already done something like
this already :)  What it does is sends in a template to use (strTemplate)
and then a variable-length string with variable-length values in each
position.  The last argument in the BreakString function is the index of the
variable-length value to retrieve from the strText string using the
strTemplate string as a template.

BreakString(strTemplate, strText, 1) would return the string "01/01/2003".
BreakString(strTemplate, strText, 2) would return the string "01:30:02".
BreakString(strTemplate, strText, 3) would return the string "MyUserName".
BreakString(strTemplate, strText, 4) would return the string "123".
BreakString(strTemplate, strText, 5) would return the string "123456".
BreakString(strTemplate, strText, 6) would return the string "127.0.0.1"
BreakString(strTemplate, strText, 7) would return the string "27015"

Now, since there can be % signs in the text, before sending the text to
BreakString, we need to check for % signs and double them up.  Then in the
BreakString function, there would need to be a check when a %s is found to
make sure it isn't really %%s.  But I think I could manage that once I get
the main functionality of BreakString to work.

I am sorry about posting to so many groups, but I believe it can actually
benefit quite a few people if this function was made and posted, and it also
applies to all of the group languages in which I posted to.  I really need
it for VB.NET but can also use it in quite a few other non-.NET
applications.

Thanks for bearing with me and thanks in advance for any and all replies!

Mythran



Sun, 31 Jul 2005 22:36:16 GMT  
 Break Apart String Using Index
I guess the following code should work (note that you have an error in
either your strTemplate or strText as there is one "<>" too much in strText
or too few in strTemplate.

Option Explicit

Dim strTemplate, strText, regEx, Matches, Match

strTemplate = "^L (.*) - (.*): ""([^<]*)<([^>]*)><([^>]*)><>"" connected,
address ""(.*):(.*)"""
strText = "L 01/01/2003 - 01:30:02: ""MyUserName<123><123456><>"" connected,
address ""127.0.0.1:27015"""

Set regEx = New RegExp
regEx.Pattern = strTemplate
Set Matches = regEx.Execute(strText)

MsgBox _
  Matches(0).SubMatches(0) & vbNewLine & _
  Matches(0).SubMatches(1) & vbNewLine & _
  Matches(0).SubMatches(2) & vbNewLine & _
  Matches(0).SubMatches(3) & vbNewLine & _
  Matches(0).SubMatches(4) & vbNewLine & _
  Matches(0).SubMatches(5) & vbNewLine & _
  Matches(0).SubMatches(6)

Regards,
Pierre


Quote:
> The following pseudo-vb is what I need and I just can't make happen:

> Dim strTemplate
> Dim strText
> Dim strValue

> strTemplate = "L %s - %s: ""%s<%s><%s>"" connected, address ""%s:%s"""
> strText = "L 01/01/2003 - 01:30:02: ""MyUserName<123><123456><>""
connected,
> address ""127.0.0.1:27015"""

> strValue = BreakString(strTemplate, strText, 1)

> Ok, the above quick code that I need and can't figure out is very
cumbersome
> for me.  It's not that I can't do it, it is just that it would take me too
> long to do it and I was wondering if someone has already done something
like
> this already :)  What it does is sends in a template to use (strTemplate)
> and then a variable-length string with variable-length values in each
> position.  The last argument in the BreakString function is the index of
the
> variable-length value to retrieve from the strText string using the
> strTemplate string as a template.

> BreakString(strTemplate, strText, 1) would return the string "01/01/2003".
> BreakString(strTemplate, strText, 2) would return the string "01:30:02".
> BreakString(strTemplate, strText, 3) would return the string "MyUserName".
> BreakString(strTemplate, strText, 4) would return the string "123".
> BreakString(strTemplate, strText, 5) would return the string "123456".
> BreakString(strTemplate, strText, 6) would return the string "127.0.0.1"
> BreakString(strTemplate, strText, 7) would return the string "27015"

> Now, since there can be % signs in the text, before sending the text to
> BreakString, we need to check for % signs and double them up.  Then in the
> BreakString function, there would need to be a check when a %s is found to
> make sure it isn't really %%s.  But I think I could manage that once I get
> the main functionality of BreakString to work.

> I am sorry about posting to so many groups, but I believe it can actually
> benefit quite a few people if this function was made and posted, and it
also
> applies to all of the group languages in which I posted to.  I really need
> it for VB.NET but can also use it in quite a few other non-.NET
> applications.

> Thanks for bearing with me and thanks in advance for any and all replies!

> Mythran



Sun, 31 Jul 2005 23:09:26 GMT  
 Break Apart String Using Index

Quote:

> The following pseudo-vb is what I need and I just can't make happen:

> Dim strTemplate
> Dim strText
> Dim strValue

> strTemplate = "L %s - %s: ""%s<%s><%s>"" connected, address ""%s:%s"""
> strText = "L 01/01/2003 - 01:30:02: ""MyUserName<123><123456><>"" connected,
> address ""127.0.0.1:27015"""

> strValue = BreakString(strTemplate, strText, 1)

> Ok, the above quick code that I need and can't figure out is very cumbersome
> for me.  It's not that I can't do it, it is just that it would take me too
> long to do it and I was wondering if someone has already done something like
> this already :)  What it does is sends in a template to use (strTemplate)
> and then a variable-length string with variable-length values in each
> position.  The last argument in the BreakString function is the index of the
> variable-length value to retrieve from the strText string using the
> strTemplate string as a template.

> BreakString(strTemplate, strText, 1) would return the string "01/01/2003".
> BreakString(strTemplate, strText, 2) would return the string "01:30:02".
> BreakString(strTemplate, strText, 3) would return the string "MyUserName".
> BreakString(strTemplate, strText, 4) would return the string "123".
> BreakString(strTemplate, strText, 5) would return the string "123456".
> BreakString(strTemplate, strText, 6) would return the string "127.0.0.1"
> BreakString(strTemplate, strText, 7) would return the string "27015"

> Now, since there can be % signs in the text, before sending the text to
> BreakString, we need to check for % signs and double them up.  Then in the
> BreakString function, there would need to be a check when a %s is found to
> make sure it isn't really %%s.  But I think I could manage that once I get
> the main functionality of BreakString to work.

> I am sorry about posting to so many groups, but I believe it can actually
> benefit quite a few people if this function was made and posted, and it also
> applies to all of the group languages in which I posted to.  I really need
> it for VB.NET but can also use it in quite a few other non-.NET
> applications.

> Thanks for bearing with me and thanks in advance for any and all replies!

> Mythran

Congratulations! Once you want to extract information from strings based on
patterns (templates), you've entered the wonderful world of regular expressions.

Introduction to Regular Expressions
http://www.*-*-*.com/

First, a note--your string doesn't match your template; there's an extra, empty
<> in the user-name section. I've adjusted the string, but you could adjust the
pattern if it's supposed to be there.

strText = "L 01/01/2003 - 01:30:02: ""MyUserName<123><123456>"" connected, address ""127.0.0.1:27015"""

With New RegExp
  .Pattern = "^L (.*?) - (.*?): ""(.*?)<(.*?)><(.*?)>"" connected, address ""(.*?):(.*?)""$"
  For Each SubMatch in .Execute(strText)(0).SubMatches
    WScript.Echo SubMatch
  Next
End With

--
Steve

Peace cannot be achieved through {*filter*}, it can only be attained through
understanding. -Ralph Waldo Emerson



Sun, 31 Jul 2005 23:33:52 GMT  
 Break Apart String Using Index
Quote:
> Congratulations! Once you want to extract information from strings based
on
> patterns (templates), you've entered the wonderful world of regular
expressions.

> Introduction to Regular Expressions

http://msdn.microsoft.com/library/en-us/script56/html/reconintroducti...
ularexpressions.asp
Quote:

> First, a note--your string doesn't match your template; there's an extra,
empty
> <> in the user-name section. I've adjusted the string, but you could
adjust the
> pattern if it's supposed to be there.

> strText = "L 01/01/2003 - 01:30:02: ""MyUserName<123><123456>"" connected,

address ""127.0.0.1:27015"""

Quote:

> With New RegExp
>   .Pattern = "^L (.*?) - (.*?): ""(.*?)<(.*?)><(.*?)>"" connected, address
""(.*?):(.*?)""$"
>   For Each SubMatch in .Execute(strText)(0).SubMatches
>     WScript.Echo SubMatch
>   Next
> End With

> --
> Steve

Hrm...maybe I wasn't clear enough :(

What I want is to be able to call a function named BreakApart or any name
will do.  This function should have THREE arguments, strTemplate, strText,
and intIndex.  So, the Template has NO regexp's in em, just the %s for the
location of the strings to extract.  Yes, I am aware it could be done using
regular expressions, and even so, it can be done inside of BreakApart using
regexp's.  But going in and coming out, I want the regexp portion to be
invisible.

Yes, I do know of regular expressions.  I am also a Perl developer and if I
claim to be a Perl developer w/o any knowledge of regexp's, I'd be in deep
dung.

Thanks for your replies, more will be helpful and welcome.

THanks again!

Mythran



Sun, 31 Jul 2005 23:45:11 GMT  
 Break Apart String Using Index

Quote:

> What I want is to be able to call a function named BreakApart or any name
> will do.  This function should have THREE arguments, strTemplate, strText,
> and intIndex.  So, the Template has NO regexp's in em, just the %s for the
> location of the strings to extract.  Yes, I am aware it could be done using
> regular expressions, and even so, it can be done inside of BreakApart using
> regexp's.  But going in and coming out, I want the regexp portion to be
> invisible.

1) Take the template argument and:
   a) escape any special regexp characters (e.g. ( ) ^ $ [ ] . \)
   b) replace all occurrences of "%s" with "(.*?)"

2) Build a regexp with the new template as the pattern.

3) Execute the regexp on the text argument.

4) Subtract 1 from the index argument.
   (Or use 0-based indexing instead of 1-based.)

5) Return the appropriate submatch.

--
Steve

Never assume the obvious is true. -William Safire



Mon, 01 Aug 2005 04:49:32 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Break Apart String Using Index

2. Break Apart String Using Index

3. Break apart (split) a string in Visual Basic/Access 97

4. breaking apart a string

5. break apart variable

6. Need Help taking a string apart - AND THANKS

7. Break single string with carriage return into 2 strings

8. Indexing is Broke?

9. VB5 Tab ocx/Index property broken?

10. Adding index and links are 'broken'

11. Arrays, string indices ... and more

12. Array object with string indexes causes length property to always return 0

 

 
Powered by phpBB® Forum Software