10 Replies - 474 Views - Last Post: 14 January 2013 - 12:16 PM Rate Topic: -----

#1 vlad__28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Highlight numbers using regex

Posted 12 January 2013 - 06:00 AM

Hello guys,
I am a beginer in vb.net and I have a question about regular expressions:

I want to highlight in a richtextbox only telephone numbers...from a phrase or a sentence.

here's my code
Dim r As Regex = New Regex("\b\d{10}", RegexOptions.Multiline)
Dim Mtch As MatchCollection = r.Matches(RichTextBox1.Text)
Dim result As String = ""
Dim i As Integer
For i = 0 To Mtch.Count - 1
Dim mat As Match = Mtch.Item(i)
result = result & vbCrLf & mat.Value



How can i link this result to rtb.select method ?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Highlight numbers using regex

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Highlight numbers using regex

Posted 12 January 2013 - 07:56 AM

How would you do the highlighting? How are you going to represent in the RichTextBox an highlighted phone number? Think about the stage involved.
Have you also done some basic research on the methods of the RichTextBox? Eg read the documentation on MSDN
Was This Post Helpful? 1
  • +
  • -

#3 vlad__28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Highlight numbers using regex

Posted 12 January 2013 - 12:58 PM

i solved it...in a way:
 Dim r As Regex = New Regex("\b\d{10}", RegexOptions.Multiline)
        Dim Mtch As MatchCollection = r.Matches(RichTextBox1.Text)
        Dim result As String = ""
        Dim i As Integer
        Dim mat As Match = Mtch.Item(i)
        For i = 0 To Mtch.Count - 1
        Next
        result = result & vbCrLf & mat.Value
        RichTextBox1.Selectionstart = mat.Index
        RichTextBox1.SelectionLength = mat.Value.Length
        RichTextBox1.SelectionColor = Color.Red



But...it highlights only first number from the rtb..if i want to highlight all numbers?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Highlight numbers using regex

Posted 12 January 2013 - 01:11 PM

Set a breakpoint on that method and step through the code, examining each variables contents. This is one form of debugging and is an essential skill to learn.

Think about what the code is actually doing versus what you think it doing. Do your expectations for what the variable contents should match reality? Does the order in which the code is run correct?

This post has been edited by AdamSpeight2008: 12 January 2013 - 01:20 PM

Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Highlight numbers using regex

Posted 12 January 2013 - 01:20 PM

It works for me. I did also remove the (redundant) For i..Next loop.

You are typing 10 numbers in sequence..?

Regex object model

Added: You will also get an error if there is no match!

This post has been edited by andrewsw: 12 January 2013 - 01:27 PM

Was This Post Helpful? 1
  • +
  • -

#6 vlad__28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Highlight numbers using regex

Posted 13 January 2013 - 12:07 AM

It is done, thanks to you guys :)
[code]
Dim r As Regex = New Regex("\b\d{10}", RegexOptions.Multiline)
Dim Mtch As MatchCollection = r.Matches(RichTextBox1.Text)
Dim result As String = ""
For i As Integer = 0 To Mtch.Count - 1
Dim mat As Match = Mtch.Item(i)
result = result & vbCrLf & mat.Value
RichTextBox1.Selectionstart = mat.Index
RichTextBox1.SelectionLength = mat.Value.Length
RichTextBox1.SelectionColor = Color.Red
[\code]
But there is one more thing...how can i save the result into an access DB?
Thakns in advance
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Highlight numbers using regex

Posted 13 January 2013 - 07:01 AM

Quote

But there is one more thing...how can i save the result into an access DB?


He, he, this is a completely different question and I think you should start a new thread. Assuming, of course, that you've already built the database..

BTW You still haven't accounted for a NoMatch in your code and you will get errors. I suggest you read that link I provided, and adapt the code in it rather than using your For loop; although, not using the For loop is just a suggestion.

This post has been edited by andrewsw: 13 January 2013 - 07:04 AM

Was This Post Helpful? 0
  • +
  • -

#8 vlad__28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Highlight numbers using regex

Posted 13 January 2013 - 07:05 AM

Dim r As Regex = New Regex("\b\d{10}", RegexOptions.Multiline)
        Dim Mtch As MatchCollection = r.Matches(RichTextBox1.Text)
        Dim result As String = ""
        For i As Integer = 0 To Mtch.Count - 1
            Dim mat As Match = Mtch.Item(i)
            result = result & vbCrLf & mat.Value
            RichTextBox1.Selectionstart = mat.Index
            RichTextBox1.SelectionLength = mat.Value.Length
            RichTextBox1.SelectionColor = Color.Red
next


But there is one more thing...how can i save the result into an access DB?
Thakns in advance

I already built the connection with a ms access DB...
This is the big question...any tutorials are welcomed :D
Was This Post Helpful? 0
  • +
  • -

#9 vlad__28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Highlight numbers using regex

Posted 13 January 2013 - 10:12 AM

Do you think that should work?
Dim r As Regex = New Regex("\b\d{10}", RegexOptions.Multiline)
	        Dim Mtch As MatchCollection = r.Matches(RichTextBox1.Text)
	        Dim result As String = ""
	        For i As Integer = 0 To Mtch.Count - 1
	            Dim mat As Match = Mtch.Item(i)
	            result = result & vbCrLf & mat.Value
	            RichTextBox1.Selectionstart = mat.Index
	            RichTextBox1.SelectionLength = mat.Value.Length
	            RichTextBox1.SelectionColor = Color.Red
dim a as string=mat.value
	next


and then i will insert value a into ms access DB
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3325
  • View blog
  • Posts: 11,246
  • Joined: 12-December 12

Re: Highlight numbers using regex

Posted 13 January 2013 - 10:27 AM

dim a as string=mat.value

You should not declare a variable within a loop. In any case, it will be over-written each time and only end-up storing the last value.

You are already storing the values in a string, separated by newlines(?):

result = result & vbCrLf & mat.Value

Comma-separators would be more usual, depending on what you will do with the data. If you will store the values as separate fields (rows) in a database then you should consider storing them in an array of some kind.
Was This Post Helpful? 0
  • +
  • -

#11 vlad__28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-November 12

Re: Highlight numbers using regex

Posted 14 January 2013 - 12:16 PM

Thank you all for the advices, it was very very helpful. :rockon:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1