473,387 Members | 1,347 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,387 software developers and data experts.

String Pattern Search Ideas

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
Nov 20 '05 #1
6 1061
Paul,
It sounds like you need a RegEx.

See System.Text.RegularExpressions.RegEx.

Hope this helps
Jay

"paul reed" <pr****@jacksonreed.com> wrote in message
news:uh**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #2
Cor
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

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

Nov 20 '05 #3
* "paul reed" <pr****@jacksonreed.com> scripsit:
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?

--
Herfried K. Wagner [MVP]
<http://dotnet.mvps.org/>
Website Address Changed!
Nov 20 '05 #4
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.

Nov 20 '05 #5
Cor
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


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.

Nov 20 '05 #6
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.

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Hank Kingwood | last post by:
This is probably an easy question, but I can't find a function (maybe my syntax is off...) to search for in a string. If someone would help out, I'd appreciate it! Also, how would you...
21
by: Chris S. | last post by:
I have a number of strings, containing wildcards (e.g. 'abc#e#' where # is anything), which I want to match with a test string (e.g 'abcdef'). What would be the best way for me to store my strings...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
7
by: Brian Mitchell | last post by:
Is there an easy way to pull a date/time stamp from a string? The DateTime stamp is located in different parts of each string and the DateTime stamp could be in different formats (mm/dd/yy or...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
3
by: Paddy | last post by:
Lets say i have a generator running that generates successive characters of a 'string' characters then I would have to 'freeze' the generator and pass the characters so far to re.search. It is...
5
by: tomasz | last post by:
Hi, here is a piece of pseudo-code (taken from Ruby) that illustrates the problem I'd like to solve in Python: str = 'abc' if str =~ /(b)/ # Check if str matches a pattern str = $` + $1 ...
0
by: Max M | last post by:
python@bdurham.com skrev: Just one tiny note: What you will be doing is a variation of the factory pattern. So this search might give you some new ideas: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.