function to strip out matching value? vb Noob

  • Thread starter Thread starter jobs
  • Start date Start date
J

jobs

I've got a string that will look like this:

zzz='hello',kkk='goodbye',jpjh='zz7t8d8',mary='Thank
You',jane='idontwantthis',somethingelse='nothinghere'

It will have undefined number comma seperated tokens.

I want to write a VB function that when passed a matching left side
identifier, returns it's assigned value. If none match, returns "Error
No Match"

so myfunction("mary") should return "Thank You"

Thanks for any help or information!
 
I've got a string that will look like this:

zzz='hello',kkk='goodbye',jpjh='zz7t8d8',mary='Thank
You',jane='idontwantthis',somethingelse='nothinghere'

It will have undefined number comma seperated tokens.

I want to write a VB function that when passed a matching left side
identifier, returns it's assigned value. If none match, returns "Error
No Match"

so myfunction("mary") should return "Thank You"

Thanks for any help or information!

hmmm you could split the string based on the comma, then go through
each item that results from the split and split again on the "=". You
could use these splits to build a hash table where the left side of
"=" is the key and the right side is the value.
Then your function would take the key, look it up in the hash table
and return the value.
 
jobs said:
I've got a string that will look like this:

zzz='hello',kkk='goodbye',jpjh='zz7t8d8',mary='Thank
You',jane='idontwantthis',somethingelse='nothinghere'

It will have undefined number comma seperated tokens.

I want to write a VB function that when passed a matching left side
identifier, returns it's assigned value. If none match, returns "Error
No Match"

so myfunction("mary") should return "Thank You"

Thanks for any help or information!

I would create a regular expression from the identifier. The identifier
"mary" would give the regular expression "(?:^|,)mary='([^']*)'".

Dim data As String =
"zzz='hello',kkk='goodbye',jpjh='zz7t8d8',mary='Thank
You',jane='idontwantthis',somethingelse='nothinghere'"
Dim identifier As String = "mary"
Dim pattern As String = "(?:^|,)" & Regex.Escape(identifier) & "='([^']*)'"
Dim match As Match = Regex.Match(data, pattern)
Dim value As String
If match.Groups.Count = 2 Then
value = match.Groups(1).Value
Else
value = "Error No Match"
End If
 
hi.

sorry, my requirements are slightly different than expected. Not sure
if the spaces or special characters are the problem, but I need it
find identifiers like this as well:

Filename = 'd:\jcp\hellocpm.txt', Filename2 = 'd:\jcp\hellocpm.txt'

its reporting match group = 1 and not 2.

Thanks.
 
hi.

sorry, my requirements are slightly different than expected. Not sure
if the spaces or special characters are the problem, but I need it
find identifiers like this as well:

Filename = 'd:\jcp\hellocpm.txt', Filename2 = 'd:\jcp\hellocpm.txt'

its reporting match group = 1 and not 2.

Thanks.

please disregard this.. I figured it out. thanks.
 
Back
Top