Need help with Regular Expressions

  • Thread starter Thread starter LBC
  • Start date Start date
L

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
 
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
 
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
 
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
 
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


| 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
|
|
| | > 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
| >
| >
| >
|
|
 
Back
Top