Q: Regular expression

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

    #1

    Q: Regular expression

    Hi

    Can anybody give me some advice on a regular expression?

    I want to get two substrings out of a string like:

    "This is a test & I hope it works"

    i.e. I want the substring before the & and the substring after the &:

    "This is a " and " I hope it works"

    Can anybody tell me what the regular expression, using Regex, is?

    Thanks in advance

    Geoff


  • Mattias Sjögren

    #2
    Re: Q: Regular expression

    >Can anybody tell me what the regular expression, using Regex, is?

    Any reason you have to use Regex for this? It would be trivial to do
    with just the String class (or VB's InStr, Left, Mid functions).



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Armin Zingler

      #3
      Re: Regular expression

      "Geoff Jones" <nodamnspam@ema il.com> schrieb[color=blue]
      >
      > Can anybody give me some advice on a regular expression?[/color]


      Have a look @ this group for language unrelated questions:
      microsoft.publi c.dotnet.framew ork


      Armin

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Regular expression

        "Geoff Jones" <nodamnspam@ema il.com> schrieb:[color=blue]
        > Can anybody give me some advice on a regular expression?
        >
        > I want to get two substrings out of a string like:
        >
        > "This is a test & I hope it works"
        >
        > i.e. I want the substring before the & and the substring after the &:
        >
        > "This is a " and " I hope it works"
        >
        > Can anybody tell me what the regular expression, using Regex, is?[/color]

        What's the reason why you don't use 'Split'?

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Dragon

          #5
          Re: Regular expression

          Hi Jeoff,

          If you really want to use regex for this job it will be:

          ~
          Dim str As String = "This is a test & I hope it works"
          Dim result() As String = Regex.Split(str , "&")
          ~

          However, if I were you I would use:

          ~
          Dim str As String = "This is a test & I hope it works"
          Dim result() As String = str.Split("&"c)
          ~

          Hope it helps

          Roman


          Comment

          • Geoff Jones

            #6
            Re: Regular expression

            Thanks everyone.

            The reason I wanted to use Regular expressions was merely to learn more
            about it i.e. I agree that the String class can be used but I wondered how
            Regex could be used.

            Thanks again

            Geoff

            "Dragon" <no@spam.please > wrote in message
            news:%23fa$w8Nn FHA.3900@TK2MSF TNGP09.phx.gbl. ..[color=blue]
            > Hi Jeoff,
            >
            > If you really want to use regex for this job it will be:
            >
            > ~
            > Dim str As String = "This is a test & I hope it works"
            > Dim result() As String = Regex.Split(str , "&")
            > ~
            >
            > However, if I were you I would use:
            >
            > ~
            > Dim str As String = "This is a test & I hope it works"
            > Dim result() As String = str.Split("&"c)
            > ~
            >
            > Hope it helps
            >
            > Roman
            >
            >[/color]


            Comment

            • Ronchese

              #7
              Re: Regular expression

              For that specific case, use:

              (\w+\s)+(?=&)|( \w+\s)+



              Explanation:

              (\w+\s)+ => any word followed by a space, one or more times
              (?=&) => conditional, specifying the next character must be a '&'
              | => optional


              so:
              (\w+\s)+(?=&) => any word followed by a space, one or more times,
              matched with a '&' character


              Use the 'The regulator' to test your expressions: http://regex.osherove.com/

              []s
              Cesar











              "Geoff Jones" <nodamnspam@ema il.com> escreveu na mensagem
              news:42f88226$0 $12883$cc9e4d1f @news.dial.pipe x.com...
              Hi

              Can anybody give me some advice on a regular expression?

              I want to get two substrings out of a string like:

              "This is a test & I hope it works"

              i.e. I want the substring before the & and the substring after the &:

              "This is a " and " I hope it works"

              Can anybody tell me what the regular expression, using Regex, is?

              Thanks in advance

              Geoff



              Comment

              Working...