Regular expression help please (hopefully simple)

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

    Regular expression help please (hopefully simple)

    I've got a file with this in it:
    [Event "Some string data"]

    The data I'd like extracted is within the quotes: Some string data

    I can read the file out and extract (using string positions) the data I'd
    like but it would neater if I use a regular expression. Only problem is I've
    never seen a working example of this type of extraction and am completely
    new to PHP.

    Thanks in advance


  • John

    #2
    Re: Regular expression help please (hopefully simple)

    "Reckless" <reckless@nospa m.com> a écrit dans le message de news:
    cj9r83$474$1@ti tan.btinternet. com...[color=blue]
    > I've got a file with this in it:
    > [Event "Some string data"]
    >
    > The data I'd like extracted is within the quotes: Some string data
    >
    > I can read the file out and extract (using string positions) the data I'd
    > like but it would neater if I use a regular expression. Only problem is[/color]
    I've[color=blue]
    > never seen a working example of this type of extraction and am completely
    > new to PHP.
    >
    > Thanks in advance
    >[/color]

    $MyString = "Event \"Some string data\"" ;
    preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;
    echo $RegArray[1] ; # should display : Some string data

    --
    John
    Own this domain today. We make your shopping experience easy. Friendly and quick customer service.



    Comment

    • Michael Fesser

      #3
      Re: Regular expression help please (hopefully simple)

      .oO(John)
      [color=blue]
      >$MyString = "Event \"Some string data\"" ;
      >preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;[/color]

      JFTR: You can also use single quotes, this avoids the ugly escaping,
      e.g.

      preg_match('/Event "(.+)"/sU', ...);

      Micha

      Comment

      • Reckless

        #4
        Re: Regular expression help please (hopefully simple)

        Hi,

        Thanks for the sample. Unfortunately I'm getting a null return value. This
        looks to be a problem as my input line has square brackets in the source
        string. If you could describe the components of the regular expression
        string - this bit: ("/^Event \"([^\"]+)\"$/") perhaps I can add them in.

        Thanks again!

        "John" <john@nospam.or g> wrote in message
        news:415872b4$0 $13505$626a14ce @news.free.fr.. .[color=blue]
        > "Reckless" <reckless@nospa m.com> a écrit dans le message de news:
        > cj9r83$474$1@ti tan.btinternet. com...[color=green]
        > > I've got a file with this in it:
        > > [Event "Some string data"]
        > >
        > > The data I'd like extracted is within the quotes: Some string data
        > >
        > > I can read the file out and extract (using string positions) the data[/color][/color]
        I'd[color=blue][color=green]
        > > like but it would neater if I use a regular expression. Only problem is[/color]
        > I've[color=green]
        > > never seen a working example of this type of extraction and am[/color][/color]
        completely[color=blue][color=green]
        > > new to PHP.
        > >
        > > Thanks in advance
        > >[/color]
        >
        > $MyString = "Event \"Some string data\"" ;
        > preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;
        > echo $RegArray[1] ; # should display : Some string data
        >
        > --
        > John
        > www.realposition.com
        >
        >[/color]


        Comment

        • Justin Koivisto

          #5
          Re: Regular expression help please (hopefully simple)

          Reckless wrote:
          [color=blue]
          > "John" <john@nospam.or g> wrote in message
          > news:415872b4$0 $13505$626a14ce @news.free.fr.. .
          >[color=green]
          >>"Reckless" <reckless@nospa m.com> a écrit dans le message de news:
          >>cj9r83$474$1@ titan.btinterne t.com...
          >>[color=darkred]
          >>>I've got a file with this in it:
          >>> [Event "Some string data"]
          >>>
          >>>The data I'd like extracted is within the quotes: Some string data
          >>>
          >>>I can read the file out and extract (using string positions) the data[/color][/color]
          > I'd[color=green][color=darkred]
          >>>like but it would neater if I use a regular expression. Only problem is[/color]
          >>
          >>I've[color=darkred]
          >>>never seen a working example of this type of extraction and am[/color][/color]
          > completely[color=green][color=darkred]
          >>>new to PHP.[/color]
          >>
          >>$MyString = "Event \"Some string data\"" ;
          >>preg_match( "/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;
          >>echo $RegArray[1] ; # should display : Some string data
          >>[/color]
          > Hi,
          >
          > Thanks for the sample. Unfortunately I'm getting a null return value. This
          > looks to be a problem as my input line has square brackets in the source
          > string. If you could describe the components of the regular expression
          > string - this bit: ("/^Event \"([^\"]+)\"$/") perhaps I can add them in.[/color]

          Try something like:
          preg_match('/^\[Event "(.*)(?!<(" \]))"\]/sU', $str, $matches);

          That would look for
          [Event "

          At the beginning of a line, then match everything until it finds"
          "]

          Then $matches[1] would have the "data"

          --
          Justin Koivisto - spam@koivi.com

          Comment

          • Reckless

            #6
            Re: Regular expression help please (hopefully simple)


            "Michael Fesser" <netizen@gmx.ne t> wrote in message
            news:d8ugl05ih9 9jfn63ilm4loep4 utbdf1beh@4ax.c om...[color=blue]
            > .oO(John)
            >[color=green]
            > >$MyString = "Event \"Some string data\"" ;
            > >preg_match("/^Event \"([^\"]+)\"$/", $MyString, $RegArray) ;[/color]
            >
            > JFTR: You can also use single quotes, this avoids the ugly escaping,
            > e.g.
            >
            > preg_match('/Event "(.+)"/sU', ...);
            >
            > Micha[/color]

            That's great thanks :)

            Any recommended reading on the syntax of those pattern match strings? TIA!


            Comment

            • kingofkolt

              #7
              Re: Regular expression help please (hopefully simple)

              "Reckless" <reckless@nospa m.com> wrote in message
              news:cj9r83$474 $1@titan.btinte rnet.com...[color=blue]
              > I've got a file with this in it:
              > [Event "Some string data"]
              >
              > The data I'd like extracted is within the quotes: Some string data
              >
              > I can read the file out and extract (using string positions) the data I'd
              > like but it would neater if I use a regular expression. Only problem is[/color]
              I've[color=blue]
              > never seen a working example of this type of extraction and am completely
              > new to PHP.
              >
              > Thanks in advance
              >
              >[/color]

              $tmp = '[Event "Some string data"]';
              $tmp2 = str_replace( array( '[Event "', '"]' ), '', $tmp );


              Comment

              • Michael Fesser

                #8
                Re: Regular expression help please (hopefully simple)

                .oO(Reckless)
                [color=blue]
                >"Michael Fesser" <netizen@gmx.ne t> wrote in message
                >news:d8ugl05ih 99jfn63ilm4loep 4utbdf1beh@4ax. com...
                >[color=green]
                >> preg_match('/Event "(.+)"/sU', ...);[/color]
                >
                >That's great thanks :)
                >
                >Any recommended reading on the syntax of those pattern match strings? TIA![/color]

                The manual? ;)

                Short explanation of the pattern above:

                / opening pattern delimiter
                Event " exact string match
                ( start of subpattern, this will be returned as result
                .. any char ...
                + ... in any number, but at least one
                ) close subpattern
                " exact string match
                / closing delimiter
                s modifier: the dot . will also match on newlines
                U modifier: all quantifiers (+ for example) turn to
                ungreedy and match the shortest possible result (default
                is greedy with matching the longest possible result)

                HTH
                Micha

                Comment

                Working...