473,395 Members | 2,446 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,395 software developers and data experts.

Need help with Regular Expressions

LBC
I just stumbled across Regular Expressions and am having problems figuring
out how to do the following:

strText = "[Total books]~dc000038~ + [Count of desks]~fa231011~ + [Number of
folders]~rs000923~"

I would like to strip out all the text within the brackets including the
brackets themselves so that the result string would be:

strText = "~dc000038~ + ~fa231011~ + ~rs000923~"

One caveat is that there may be additional square brackets in the string
that are not followed by a "~xx######~" that I would need to keep as part of
the string.

Can anyone help?

Tia,
LBC
Nov 20 '05 #1
4 1070
try this:

Imports System.Text.RegularExpressions

#Region " regular expressions "

Private Function parsedText() As String
Dim testString As String = "[Total books]~dc000038~ + [Count of
desks]~fa231011~ + [Number of folders]~rs000923~ [Chicken Scratchings]"
Dim labelPattern As String = "(\[[^\]]*?\]){1}?"
Dim valuePattern As String = "(~[a-z]{2}\d+~){1}?"
Dim fullPattern As String = "(" & labelPattern & valuePattern & ")+?"
Dim options As RegexOptions = RegexOptions.ExplicitCapture Or
RegexOptions.IgnoreCase Or RegexOptions.Singleline
Dim regExp As New Regex(fullPattern, options)
If Not regExp.IsMatch(testString) Then Return Nothing
Return regExp.Replace(testString, labelPattern & "[^~]*?~{1}", "~")
End Function

#End Region

Nov 20 '05 #2
sorry...whipped that one out to quickly and just now realized i had an error
in the pattern. here's the fix.

Private Function parsedText() As String
Dim testString As String = "[Total books]~dc000038~ + [Chicken
Scratchings] + [Count of desks]~fa231011~ + [Number of folders]~rs000923~"
Dim labelPattern As String = "(\[[^\]]*?\]\s*~){1}?"
Dim valuePattern As String = "(\s*[a-z]{2}\d+\s*~){1}?"
Dim fullPattern As String = "(" & labelPattern & valuePattern & ")+?"
Dim options As RegexOptions = RegexOptions.ExplicitCapture Or
RegexOptions.IgnoreCase Or RegexOptions.Singleline
Dim regExp As New Regex(fullPattern, options)
If Not regExp.IsMatch(testString) Then Return Nothing
Return regExp.Replace(testString, labelPattern, "~")
End Function

hth,

steve
Nov 20 '05 #3
LBC
Wow... worked like a champ. Thanks so much! Can you recommend a good
reference for learning about Regular Expressions other than MSDN?

Again, thanks!!

LBC
"steve" <as*@abc.com> wrote in message
news:vp************@corp.supernews.com...
try this:

Imports System.Text.RegularExpressions

#Region " regular expressions "

Private Function parsedText() As String
Dim testString As String = "[Total books]~dc000038~ + [Count of
desks]~fa231011~ + [Number of folders]~rs000923~ [Chicken Scratchings]"
Dim labelPattern As String = "(\[[^\]]*?\]){1}?"
Dim valuePattern As String = "(~[a-z]{2}\d+~){1}?"
Dim fullPattern As String = "(" & labelPattern & valuePattern & ")+?"
Dim options As RegexOptions = RegexOptions.ExplicitCapture Or
RegexOptions.IgnoreCase Or RegexOptions.Singleline
Dim regExp As New Regex(fullPattern, options)
If Not regExp.IsMatch(testString) Then Return Nothing
Return regExp.Replace(testString, labelPattern & "[^~]*?~{1}", "~")
End Function

#End Region

Nov 20 '05 #4
try these:

http://www.evolt.org/article/rating/20/22700/

that one has good references at the bottom of the page too.

http://aspnet.4guysfromrolla.com/articles/022603-1.aspx

that one is good for covering beggining basics but is still very
comprehensive.

i think i'll write my own just to facilitate "valid" pattern creation based
on a string having the pattern i'm trying to match...much like we did here.
but that may be for another day.

glad it works.

steve
"LBC" <th**********@mindspring.com> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
| Wow... worked like a champ. Thanks so much! Can you recommend a good
| reference for learning about Regular Expressions other than MSDN?
|
| Again, thanks!!
|
| LBC
|
|
| "steve" <as*@abc.com> wrote in message
| news:vp************@corp.supernews.com...
| > try this:
| >
| > Imports System.Text.RegularExpressions
| >
| > #Region " regular expressions "
| >
| > Private Function parsedText() As String
| > Dim testString As String = "[Total books]~dc000038~ + [Count of
| > desks]~fa231011~ + [Number of folders]~rs000923~ [Chicken Scratchings]"
| > Dim labelPattern As String = "(\[[^\]]*?\]){1}?"
| > Dim valuePattern As String = "(~[a-z]{2}\d+~){1}?"
| > Dim fullPattern As String = "(" & labelPattern & valuePattern & ")+?"
| > Dim options As RegexOptions = RegexOptions.ExplicitCapture Or
| > RegexOptions.IgnoreCase Or RegexOptions.Singleline
| > Dim regExp As New Regex(fullPattern, options)
| > If Not regExp.IsMatch(testString) Then Return Nothing
| > Return regExp.Replace(testString, labelPattern & "[^~]*?~{1}", "~")
| > End Function
| >
| > #End Region
| >
| >
| >
|
|
Nov 20 '05 #5

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

Similar topics

1
by: Showjumper | last post by:
I need a regulr expression that cn be used to find <a> tags with the href attribute set to mailto:. I need to be to search out all the email addresses in the text of the pages in a website. So for...
3
by: Joe | last post by:
Hi, I have been using a regular expression that I don’t uite understand to filter the valid email address. My regular expression is as follows: <asp:RegularExpressionValidator...
5
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ...
2
by: news.microsoft.com | last post by:
I need help design a reg exp. I am parsing an html file to get the input values, here is one example <input VALUE="Staff Writer" size=60 type="text" name="author"> Can I grab the value "Staff...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
14
by: Chris | last post by:
I need a pattern that matches a string that has the same number of '(' as ')': findall( compile('...'), '42^((2x+2)sin(x)) + (log(2)/log(5))' ) = Can anybody help me out? Thanks for any help!
2
by: Prakash | last post by:
Dear Friends, In my web application i have two textboxes to collect "ORIGIN" and "DESTINATION" stations. These stations are represented as three letter code.. We have an option like to provide...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
22
by: Chuck Connors | last post by:
Hey guys. I'm working on a little program to help my wife catalog her/ our coupons. I found a good resource but need help formatting the text data so that I can import it into a mysql database....
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.