regular expression question?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leon

    regular expression question?

    why is the following not correct in asp.net? I'm trying to match all
    subdomain names 'leon.domain.co m', but
    not 'www.domain.com '?
    Dim sdm As Regex

    sdm = New Regex (?!www\.)(.*)\. domain\.com


  • tom.pesterDeLeTeTHISSS@pandora.be

    #2
    Re: regular expression question?


    The regex below did work for me. Hope this helps..

    Dim re As New Regex("(?!www\. )([^\s]*?)\.domain\.co m")


    Dim matches As MatchCollection = re.Matches("zer zaerza rr.domain.com
    sfdsqf pp.domain.com ")
    Dim match As Match

    For Each match In matches
    Response.Write( "-" & match.Groups(1) .ToString)
    Next


    Cheers,
    Tom Pester
    [color=blue]
    > why is the following not correct in asp.net? I'm trying to match all
    > subdomain names 'leon.domain.co m', but
    > not 'www.domain.com '?
    > Dim sdm As Regex
    > sdm = New Regex (?!www\.)(.*)\. domain\.com
    >[/color]


    Comment

    • Leon

      #3
      Re: regular expression question?

      what can i do to the following to make work the following way?

      the user subdomain = tom.domain.com

      Current Context.Items(" Test") Results =
      /Testing/Test1.aspx?Cons ultantId=tom/default.aspx

      Desired Context.Items(" Test") Results =
      /Testing/Test1.aspx?Cons ultantId=tom

      how?

      *My Current Code....
      Dim subdomain As String = Request.Url.ToS tring

      Dim sdm As Regex

      sdm = New Regex ("http://(?!www\.)(.*)(\ .vipcarsales\.c om)")

      Context.Items(" Test") = (sdm.Replace(su bdomain ,
      "/Testing/Test1.aspx?Cons ultantId=$1"))



      <tom.pesterDeLe TeTHISSS@pandor a.be> wrote in message
      news:a1a977a21e 3008c73e3d4a9eb a0c@news.micros oft.com...[color=blue]
      >
      > The regex below did work for me. Hope this helps..
      >
      > Dim re As New Regex("(?!www\. )([^\s]*?)\.domain\.co m")
      > Dim matches As MatchCollection = re.Matches("zer zaerza
      > rr.domain.com sfdsqf pp.domain.com ")
      > Dim match As Match
      > For Each match In matches
      > Response.Write( "-" & match.Groups(1) .ToString)
      > Next
      >
      >
      > Cheers,
      > Tom Pester
      >[color=green]
      >> why is the following not correct in asp.net? I'm trying to match all
      >> subdomain names 'leon.domain.co m', but
      >> not 'www.domain.com '?
      >> Dim sdm As Regex
      >> sdm = New Regex (?!www\.)(.*)\. domain\.com
      >>[/color]
      >
      >[/color]


      Comment

      • tom.pesterDeLeTeTHISSS@pandora.be

        #4
        Re: regular expression question?


        The non matching text was appended so I expanded the 2nd group to match everything
        that comes after the .com

        sdm = New Regex("http://(?!www\.)(.*)(\ .vipcarsales\.c om.*)")

        Cheers,
        Tom Peste


        Comment

        • Cowboy (Gregory A. Beamer) - MVP

          #5
          RE: regular expression question?

          I find it easier to start with two question:

          1. Is it an URL
          2. Does it start with www

          The answer is to figure out first if it is an url and then exclude if it has
          a match on www. The following is not a final answer, but it will answer the
          question you are looking to answer:

          //C# version
          private static bool IsNonWwwUrl(str ing url)
          {
          Regex wwwRegex = new Regex(
          @"([!www]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
          Regex urlRegex = new Regex(
          @"([\w-]+\.)+[\\w-]+(/[\w- ./?%&=]*)?");

          Return ((urlRegex.IsMa tch(url)&&
          (!wwwRegex.IsMa tch(url));
          }

          'VB.NET answer
          Function IsNonWwwUrl(ByR ef url As String) As Boolean
          'this one should not match
          Dim wwwRegex As New Regex(_
          "([!www]+\.)+[\w-]+(/[\w- ./?%&=]*)?")

          'this one should match
          Dim urlRegex As New Regex(_
          "([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?")

          Return ((urlRegex.IsMa tch(url)) And _
          Not wwwRegex.IsMatc h(url)))
          End Function

          If you want to include the possibility of http:// and https:// you will look
          for number of matches rather than match the entire string (this is C# only,
          apologies):

          private static bool IsNonWwwUrl(str ing url)
          {
          Regex wwwRegex = new Regex(
          @"([!www]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
          Regex urlRegex = new Regex(
          @"([\w-]+\.)+[\\w-]+(/[\w- ./?%&=]*)?");

          if((wwwRegex.Ma tches(url) == 0)&&
          (urlRegex.Match es(url) != 0))
          return true;
          else
          return false;
          }



          --
          Gregory A. Beamer
          MVP; MCP: +I, SE, SD, DBA

          *************** ************
          Think Outside the Box!
          *************** ************


          "Leon" wrote:
          [color=blue]
          > why is the following not correct in asp.net? I'm trying to match all
          > subdomain names 'leon.domain.co m', but
          > not 'www.domain.com '?
          > Dim sdm As Regex
          >
          > sdm = New Regex (?!www\.)(.*)\. domain\.com
          >
          >
          >[/color]

          Comment

          Working...