String Pattern Search Ideas

  • Thread starter Thread starter Jay B. Harlow [MVP - Outlook]
  • Start date Start date
J

Jay B. Harlow [MVP - Outlook]

Paul,
It sounds like you need a RegEx.

See System.Text.RegularExpressions.RegEx.

Hope this helps
Jay
 
Hello,

I need to retrieve strings that are bounded by special characters from a
larger string **item1%%**item2%%**item3%%.

What is the most efficient way to do this?

Thanks,

Paul
 
Hi Paul,

In addition to Jay have also a look what just the replace can do for you.

(When you replace strings is the fastest the Microsoft Visual Basic replace,
when it are single character which have to be replaced, you can beter use
the string.replace (and tell that it is a character by using "%"c).

I hope this helps.

Cor
 
* "paul reed said:
I need to retrieve strings that are bounded by special characters from a
larger string **item1%%**item2%%**item3%%.

Doesn't the string's 'Split' method do the job for you?
 
Hi Paul,


If you just need to return the substrings delimited by "%%", the split is a
quick through.
Also the regex will give you more flexible function, but you need to know
the pattern first.

Here goes the codes
Dim myString As String = "**item1%%**item2%%**item3%%"
'return **item1,**item2,**item3, use split method
Dim myArray() As String = Split(myString, "%%", ,
CompareMethod.Text)


'use the regex method will be more flexible.
Dim r As Regex = New Regex("(item[0-9])%%", RegexOptions.IgnoreCase)
Dim mc As MatchCollection = r.Matches(myString)
For i As Integer = 0 To mc.Count - 1
'writeout item1,item2,item3,
Console.WriteLine(mc(i).Groups(1).ToString)
Next


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

Is there a special reason that you take the answers from 2 MVP's as the
right answer and tell with that explictly that mine is wrong.

The split for this is come in this newsgroup from a joke from me, because I
saw everybody was giving an answer on a very simple question and I thought I
can make even another one. It works, but it is very slow when the string
become longer. (What I did prove later in the same thread)

I did give the advice to take the replace.
Try this
\\\
Dim oldstring As String = "**item1%%**item2%%**item3%%"
Dim newstring As String = Replace(oldstring, "**", " ")
newstring = Replace(newstring, "%%", "")
' or if you like this
Dim alternativesting As String = oldstring.Replace("**", " ").Replace("%%",
"")
///
And than test them all on the performance, I am quiet sure that the first
method although it goes twice times through it, will be the fastest.

(I have done tests with all the methods that are now described)

Cor
 
Hi Cor,

Thank you for your sharing the knowlege with us.

I am sorry if I have any misunderstanding with the original post.

I just think that the the "**" in the string below may be any characters,
not the specified character "*"
**item1%%**item2%%**item3%%

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top