Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Reg Exp VB.NET

  1. #1
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts

    Reg Exp VB.NET

    Okay,
    I am working on a application at work for a helpdesk. My code freeze is Friday and I am trying to add in some neato features


    Right now I am working on formatting the text, I basically have all of the HTML tags being converted over to BIU and some others but I am struggling to do the following in a small amount of code:

    Say I have a string with a URL in it

    Code:
    Dim str1 As String = "Look at the following page: http://theURL.com for more details"
    using a generic reg exp to match any URL would look like this???( I think)

    Code:
    veRegEx = New Regex("http://\s[3,]")
    but how would I go about matching it but do the following

    Code:
    Dim str1 As String = "Look at the following page: <a href='http://theURL.com' target='_blank'>http://theURL.com</a> for more details"
    I am just lost for ideas since I really suck at reg. exp.

    I know a really long way to do it without reg. exp. but that is not fun!

    Little challange for you all!
    Thanks
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    okay I almost got it

    Code:
    ticket_body = Regex.Replace(ticket_body, "(\bhttp[s]{0,1}://[^ ]+\b)", "<a href=""$0"">$0</a>")

    The only problem is it will not stop at the end of the line and takes over tags too

    Maybe someone can help me with that....
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Haven't had time to mess with it, but have you tried leaving out the word boundary since you're already using "not space", or vice versa? Just an idea.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, this works in javascript, may not be the best way but it tested ok:

    Code:
    <html>
    	<head>
    		<title></title>
    	</head>
    	<body>
    <script type="text/javascript">
    <!--
    var str = "Look at the following page: http://theURL.com for more details.";
    var regExp = /(https?:\/\/[^ ]*)/
    //alert(regExp.test(str));
    alert(str.replace(regExp,"<a href=\"$1\">$1</a>"));
    document.write(str.replace(regExp,"<a href=\"$1\">$1</a>"));
    // -->
    </script>
    	</body>
    </html>
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The reason why is some of those other characters are counted as word boundaries. They don't match the regular expression definition of a "word character", which is either alphabetical, numeric, or an underscore ONLY, i.e.:

    [A-Za-z0-9_]

    or

    [\w]

    The second is just a shortcut to match word characters.

    Since there are slashes, colons, decimals, quotes, etc. in a URL, they're going to match \b - which will return only a partial match (and to be honest I'm having a hard time figuring out a way to work it like that, since in order to exclude "non-URL" characters, you would have to include every character that doesn't belong in a particular spot in a URL, which breaks the golden rule of K.I.S.S.)

    I think if you can just make sure you encode the url when necessary if there's a querystring involved, that should take care of any spaces, then that regex is good to go... and there might still be a better one already written.

    Here's a pretty nifty article (and website) I just found:

    http://www.regular-expressions.info/wordboundaries.html

    (P.S. This looks like it might be a worthy link...). Despite what they say, regular expressions are pretty similar (and normally close or absolutely identical in syntax) in almost all languages, including C# and VB.NET.
    Last edited by whammy; 11-18-2003 at 03:53 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    By the way that text looks and sounds awkward (although I'm aware that it's probably your client's choice of words):

    "Look at the following page: http://theURL.com for more details."



    P.S. Where's some of your new and improved programs Alien51?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You can not see my programs,....they are all on the intranet

    My latest creation is a helpdesk software that our company uses coded in .NET.

    I figured out the code last friday, my mistake was I was looking for < when I should have been looking for breaks \n

    That was my whole mistake, but I got it to work.

    I am working on a database design tool in my soare time that wil allow a person to design a database with a psyical model so they can see all the primary keys and foreign keys and restraints.

    I am trying to code this in .Net and XML for storage, bu I have been too busy at work to code it.

    Another progect is an AI tic tac toe player that learns from playing games. You give it a few basic rules and it will play and memorize good and bad combinations. Hopefully after a long series of games it should know the best combinations.

    I also work on some business software at my work. My job on that is to make the NEAT-O features that know one else can figure out how to code. Plus I do most of the JavaScript work.

    (And I was hired as Application Support which I still do - fix bugs and user errors in 8+ applications)

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #9
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    My days are too short!
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •