12 Replies - 1736 Views - Last Post: 20 March 2011 - 09:53 AM Rate Topic: -----

#1 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Need Urgent Help [Regex]

Posted 13 March 2011 - 09:47 AM

Hi Guyz! This is my first post and i am learning vb.net

So here is my piece of code.

Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim htmltags2 As String = TextBox1.Text

        ' Result.
        Dim tagles2 As String = StripTagsvb2learn(htmltags2)

        ' Write.
        Console.WriteLine(tagles2)

        TextBox1.Text = tagles2
    End Sub
    Function StripTagsvb2learn(ByVal html As String) As String
        ' Remove HTML tags.
        Return Regex.Replace(html, "](.*?)[/url]", "")
    End Function
End Class




GUI:
Posted Image

So if i enter this text in textbox1

http://www.dreamincode.net/forums]Download part 1[/url]
http://www.dreamincode.net]Download part 1[/url]


Then it returns:
http://www.dreamincode.net/forumsoad part 1[/url]
http://www.dreamincode.netoad part 1[/url]



Now i want it to be like this:

http://www.dreamincode.net/forums
http://www.dreamincode.net


Now cant get further.
Hoping for a fast reply guyz

Is This A Good Question/Topic? 0
  • +

Replies To: Need Urgent Help [Regex]

#2 Jack Eagles1   User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 187
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Need Urgent Help [Regex]

Posted 13 March 2011 - 10:33 AM

Well if you just want to get the base URL (host) whatever text is entered, something like this will work:

Dim myuri As New Uri(TextBox1.Text)
TextBox1.Text = myuri.Host


With this, if the user enters:
http://www.dreamincode.net/forums/whatever.html

It will return
www.dreamincode.net


If that's not what you're looking for, then I'd try this:
Dim lastIndexOfSlash As String = TextBox1.Text.Substring(TextBox1.Text.LastIndexOf("/"))
TextBox1.Text = TextBox1.Text.Replace(lastIndexOfSlash, "")


With this, if the user enters:
http://www.dreamincode.net/forums/whatever.html

It will return:
http://www.dreamincode.net/forums


And if the user enters:
http://www.dreamincode.net/whatever/whateverelse/something.html

It will return:
http://www.dreamincode.net/whatever/whateverelse



I'm not sure if I got the question quite right, so do tell me if I didn't.

This post has been edited by Jack Eagles1: 13 March 2011 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#3 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Re: Need Urgent Help [Regex]

Posted 14 March 2011 - 04:01 AM

@Jack Eagles 1
Thanks for your help but its not what i wanted.

I wanted to remove everything between [ and [/url] in

http://www.dreaminco...forums]Download part 1[/url]
http://www.dreamincode.net]Download part 1[/url]


Url Will change everytime and the text between ] and [/url] will also.
Therefore i think that it will be possible only with regex.

But in my code, its not replacing all the things
Was This Post Helpful? 0
  • +
  • -

#4 xnn   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 238
  • Joined: 10-February 10

Re: Need Urgent Help [Regex]

Posted 14 March 2011 - 07:30 AM

I was able to extract the URL using Matching instead of regex replace.

I used the following code:
    Dim sURI As String = "http://www.dreamincode.net/forums]Download part 1[/url] "

    Dim oMatch As Match = New Regex("^(?<URL>[^]]*)").Match(sURI)

    MessageBox.Show(oMatch.Groups("URL").Value)



I used the following code to extract the URI using regex.replace
Regex.Replace(sURI, "].*", "")



In both, if a closing bracket is part of the URL they will fail. They will match up until that bracket
Was This Post Helpful? 0
  • +
  • -

#5 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Re: Need Urgent Help [Regex]

Posted 14 March 2011 - 09:27 AM

View Postxnn, on 14 March 2011 - 07:30 AM, said:

I was able to extract the URL using Matching instead of regex replace.

I used the following code:
    Dim sURI As String = "http://www.dreamincode.net/forums]Download part 1[/url] "

    Dim oMatch As Match = New Regex("^(?<URL>[^]]*)").Match(sURI)

    MessageBox.Show(oMatch.Groups("URL").Value)



I used the following code to extract the URI using regex.replace
Regex.Replace(sURI, "].*", "")



In both, if a closing bracket is part of the URL they will fail. They will match up until that bracket


Hello But if i enter this as then it removes other lines too :(


http://www.dreaminco...forums]Download part 1[/url]
http://www.dreamincode.net]Download part 1[/url]

This post has been edited by dreamvb: 14 March 2011 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#6 xnn   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 238
  • Joined: 10-February 10

Re: Need Urgent Help [Regex]

Posted 14 March 2011 - 09:54 AM

How are the URLs delimited? a Carriage return?
Was This Post Helpful? 0
  • +
  • -

#7 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Re: Need Urgent Help [Regex]

Posted 14 March 2011 - 10:13 AM

View Postxnn, on 14 March 2011 - 09:54 AM, said:

How are the URLs delimited? a Carriage return?

Hello The Url Always starts with:
"" Then there is hyperlinked text which is ended by

So here i want to remove everything between "]" and "[/url]"
Was This Post Helpful? 0
  • +
  • -

#8 xnn   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 238
  • Joined: 10-February 10

Re: Need Urgent Help [Regex]

Posted 14 March 2011 - 11:20 AM

I think you are missing some text in your previous two posts.


If your URL always starts with "http://" and if you can assume that a closing bracket will not be in the url then the following should work.

    Dim sURI As String = "http://www.dreamincode.net/forums1]Download part 1[/url]http://www.dreamincoded.net/forums2]Download part 1[/url]"

    Dim oMatches As MatchCollection = New Regex("(?<URL>http://[^]]*)").Matches(sURI)

    For Each oMatch As Match In oMatches
      MessageBox.Show(oMatch.Groups("URL").Value)
    Next


This post has been edited by xnn: 14 March 2011 - 11:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Re: Need Urgent Help [Regex]

Posted 15 March 2011 - 12:22 AM

View Postxnn, on 14 March 2011 - 11:20 AM, said:

I think you are missing some text in your previous two posts.


If your URL always starts with "http://" and if you can assume that a closing bracket will not be in the url then the following should work.

    Dim sURI As String = "http://www.dreamincode.net/forums1]Download part 1[/url]http://www.dreamincoded.net/forums2]Download part 1[/url]"

    Dim oMatches As MatchCollection = New Regex("(?<URL>http://[^]]*)").Matches(sURI)

    For Each oMatch As Match In oMatches
      MessageBox.Show(oMatch.Groups("URL").Value)
    Next



Hello Mate
Your code is just perfect.
But it shows the message box whereas i want it to be in the textbox back.

I tried this:

  TextBox1.Text = oMatch.Groups("URL").Value


Instead Of :

 MessageBox.Show(oMatch.Groups("URL").Value)


But still its not working.

And in my case the url starts with [url= and ends with the bracke ([)

This post has been edited by dreamvb: 15 March 2011 - 12:23 AM

Was This Post Helpful? 0
  • +
  • -

#10 xnn   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 238
  • Joined: 10-February 10

Re: Need Urgent Help [Regex]

Posted 15 March 2011 - 06:15 AM

Could you please post the code you are trying along with the exact input?

You want the captured URLs to populate a textbox? Do you want them separated by a new line, space, tab, or another character?
Was This Post Helpful? 0
  • +
  • -

#11 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Re: Need Urgent Help [Regex]

Posted 15 March 2011 - 06:58 AM

View Postxnn, on 15 March 2011 - 06:15 AM, said:

Could you please post the code you are trying along with the exact input?

You want the captured URLs to populate a textbox? Do you want them separated by a new line, space, tab, or another character?

Hello
The lines in the textbox will be having space/one new line by default. So no need of that.

Just i want like this, eg:

This is the value in textbox1 by default:

http://www.dreamincode.net/forums1]Download part 1[/url]
http://www.dreamincoded.net/forums2]Download part 1[/url]


On cliciking button1, it will replace in textbox1 only too:

http://www.dreamincode.net/forums1
http://www.dreamincoded.net/forums2


Thanx
Was This Post Helpful? 0
  • +
  • -

#12 xnn   User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 238
  • Joined: 10-February 10

Re: Need Urgent Help [Regex]

Posted 15 March 2011 - 12:44 PM

Regex Named Captures:

Dim sURIs as String = Textbox1.text
Dim sbWorkingURLs as new System.Text.StringBuilder()

Dim oMatches As MatchCollection = _ 
    New Regex("http://(?<URL>[^]]*)").Matches(sURI)

'If you don't want the URL's delimited by a new line use .Append(oMatch.Groups("URL").Value)
'.Append(##Delimiter Here##)
For Each oMatch As Match In oMatches
sbWorkingURLs.appendLine(oMatch.Groups("URL").Value)
Next

TextBox1.Text = sbWorkingURLS.tostring



Regex Replace:
Textbox1.text = Regex.Replace(Textbox1.Text, "][^]]+]", "")



Either should work. Goodluck.

This post has been edited by xnn: 15 March 2011 - 12:45 PM

Was This Post Helpful? 1
  • +
  • -

#13 dreamvb   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 13-March 11

Re: Need Urgent Help [Regex]

Posted 20 March 2011 - 09:53 AM

Thank you xnn
That perfectly worked (1st method)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1