How do I make a regex that can detect a certain character string that is either all by its self in a line, or separated by white space.
If I have the line:
"ot"
Or the line
"sdf gdfg dv ewrsef fvdf ot sdfsd sdot"
I want to be able to detect the 'ot', except for the one that is 'sdot'
How do I do that?
Regular ExpressionHow do I make one
Page 1 of 1
5 Replies - 2697 Views - Last Post: 01 December 2010 - 10:14 AM
Replies To: Regular Expression
#2
Re: Regular Expression
Posted 01 December 2010 - 09:20 AM
My code so far:
I think that "/b./b" might be close to what I need??
Dim strRegex As String = ""
Dim re As New System.Text.RegularExpressions.Regex(strRegex)
Try
For Count = 0 To (Lingo.Length / 2) - 2
If re.IsMatch(Lingo(Count, 0)) Or re.IsMatch(Lingo(Count, 0).ToLower) Then
msg.Body = strRegex.Replace(msg.Body, Lingo(Count, 1).ToUpper)
End If
Next
Catch ex As Exception
End Try
I think that "/b./b" might be close to what I need??
This post has been edited by vbnet9: 01 December 2010 - 09:21 AM
#3
Re: Regular Expression
Posted 01 December 2010 - 09:56 AM
use the boundary anchor
\b
it's escaped to differentiate from the letter b
example:
should result in:
note mat1 skips over the first 'ot' that is inside of a word and goes to the next one at index 26.
yet mat2 still works even though there is no whitespace around it.
You can find ending with and starting with by just putting the boundary anchor on the right or left respectively.
Here is an article on the boundary anchor specifically
http://www.regular-e...boundaries.html
That website is really great for Regex and I visit there ALL the time if I need to look up some type of regular expression command I don't know.
\b
it's escaped to differentiate from the letter b
example:
Dim str1 As String = "sdf gdotfg dv ewrsef fvdf ot sdfsd sdot"
Dim str2 As String = "ot"
Dim rx As New Regex("\bot\b")
Dim mat1 As Match = rx.Match(str1)
Dim mat2 As Match = rx.Match(str2)
Console.WriteLine(mat1.Success & " -- " & mat1.Index & " -- " & mat1.Value)
Console.WriteLine(mat2.Success & " -- " & mat2.Index & " -- " & mat2.Value)
should result in:
Quote
True -- 26 -- ot
True -- 0 -- ot
True -- 0 -- ot
note mat1 skips over the first 'ot' that is inside of a word and goes to the next one at index 26.
yet mat2 still works even though there is no whitespace around it.
You can find ending with and starting with by just putting the boundary anchor on the right or left respectively.
Here is an article on the boundary anchor specifically
http://www.regular-e...boundaries.html
That website is really great for Regex and I visit there ALL the time if I need to look up some type of regular expression command I don't know.
This post has been edited by lordofduct: 01 December 2010 - 09:59 AM
#4
Re: Regular Expression
Posted 01 December 2010 - 09:59 AM
Well I have done this now,
But I can't get both UPPER and LOWER case versions of the word to be detected. How do I do that.
For Count = 0 To (Lingo.Length / 2) - 2
Dim strRegex As String = "\b" & Lingo(Count, 0) & "\b"
Dim re As New System.Text.RegularExpressions.Regex(strRegex)
If re.IsMatch(Lingo(Count, 0)) Or re.IsMatch(Lingo(Count, 0).ToLower) Then
msg.Body = re.Replace(msg.Body, Lingo(Count, 1).ToUpper)
msg.Body = re.Replace(msg.Body, Lingo(Count, 1).ToLower)
End If
Next
But I can't get both UPPER and LOWER case versions of the word to be detected. How do I do that.
#5
Re: Regular Expression
Posted 01 December 2010 - 10:03 AM
When you create the RegEx set the 'ignorecase' bit flag:
A regular expression is usually capable of doing almost all the work of the search for you. There shouldn't be a need of some case like you have there (if match upper OR match lower)... the RegEx is almost always capable of checking anything character specific (with some exceptions of course... they aren't magic).
Dim rx As New Regex("\bot\b", RegexOptions.IgnoreCase)
A regular expression is usually capable of doing almost all the work of the search for you. There shouldn't be a need of some case like you have there (if match upper OR match lower)... the RegEx is almost always capable of checking anything character specific (with some exceptions of course... they aren't magic).
This post has been edited by lordofduct: 01 December 2010 - 10:05 AM
#6
Re: Regular Expression
Posted 01 December 2010 - 10:14 AM
Wow, you know, you are really awesome.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|