Regular Expression

How do I make one

Page 1 of 1

5 Replies - 3409 Views - Last Post: 01 December 2010 - 10:14 AM Rate Topic: -----

#1 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Regular Expression

Posted 01 December 2010 - 09:12 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Regular Expression

#2 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Regular Expression

Posted 01 December 2010 - 09:20 AM

My code so far:

 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

Was This Post Helpful? 0
  • +
  • -

#3 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

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:

        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


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

Was This Post Helpful? 2
  • +
  • -

#4 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Regular Expression

Posted 01 December 2010 - 09:59 AM

Well I have done this now,

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.
Was This Post Helpful? 0
  • +
  • -

#5 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Regular Expression

Posted 01 December 2010 - 10:03 AM

When you create the RegEx set the 'ignorecase' bit flag:

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

Was This Post Helpful? 2
  • +
  • -

#6 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Regular Expression

Posted 01 December 2010 - 10:14 AM

Wow, you know, you are really awesome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1