RegExp : using end-of-line in a pattern

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

    RegExp : using end-of-line in a pattern

    Hi to all,
    I would like to get field values from a multi-line text.

    For instance :
    "
    Name: Mr Smith
    Address: Paris
    "

    I've tried to use (I'm a total regexp newbie, so I took it from the doc):
    eregi("Name: (.*)\n",$Variab leHoldingTheTex t,$parts);

    Problem: $parts[1] starts at the right place, but does not stop at the
    end of the line. I get :
    "Mr Smith
    Address: Paris
    "

    Where am I wrong ? I've tried to google around, but didn't find anything
    that could help me...

    Thanks in advance for your help/hints/links !

    BR
    --
    Damien
  • Zilla

    #2
    Re: RegExp : using end-of-line in a pattern

    Damien wrote:[color=blue]
    > Hi to all,
    > I would like to get field values from a multi-line text.
    >
    > For instance :
    > "
    > Name: Mr Smith
    > Address: Paris
    > "
    >[/color]

    Im not sure what you want, but if you want to get the different lines in
    different variables, then you can use the explode()-function to split
    the string:

    $lines = explode("\n", $string);

    The above code would give an array $lines where each array-element
    contains one line of $string.

    If $string contains

    "Name: Mr Smith
    Address: Paris"

    then $lines[0] contains "Name: Mr Smith" and $lines[1] contains
    "Address: Paris".

    More information on the explode()-function: www.php.net/explode

    Zilla

    Comment

    • Damien

      #3
      Re: RegExp : using end-of-line in a pattern

      Zilla a écrit :
      (snip)
      [color=blue]
      > Im not sure what you want, but if you want to get the different lines in
      > different variables, then you can use the explode()-function to split
      > the string:[/color]
      (snip)
      Thanks for this. I have several values to look for, and I don't know in
      what lines they are (imagine a form lost in the middle of a text). This
      is why I did not break up the text in lines. I'd rather not look for
      evey value in every line...

      Any other idea ?
      BR,
      Damien

      Comment

      • Zilla

        #4
        Re: RegExp : using end-of-line in a pattern

        Damien wrote:
        ....[color=blue]
        > Thanks for this. I have several values to look for, and I don't know in
        > what lines they are (imagine a form lost in the middle of a text). This
        > is why I did not break up the text in lines. I'd rather not look for
        > evey value in every line...[/color]

        I think I found out whats wrong. I think I have read some place that the
        function looks for the largest possible pattern. So if you are looking
        for some a letter followed by a : in the following:

        "x:y:z"

        it will return "x:y:" and not "x:" as you were looking for.

        So in your example there must be a newline after "Adress: Paris" so it
        takes this line too.

        When that is said, i can't figure out just now, how to prevent it from
        doing that. But I'll think about it and get back later if i figure it out.

        Zilla

        Comment

        • Chung Leong

          #5
          Re: RegExp : using end-of-line in a pattern

          I would recommend using the Perl compatible regexp functions over the
          POSIX ones. They're faster and more powerful. People also tend to be
          more familiar with the Perl syntax.

          The following snippet does what you want:

          <?
          $s = "
          Name: Mr Smith
          Address: Paris
          ";
          preg_match_all( '/^(.+?): (.+?)$/m', $s, $m, PREG_SET_ORDER) ;
          foreach($m as $set) {
          echo "{$set[1]} = {$set[2]}<br>";
          }
          ?>

          The m modifier at the end tells the regexp engine to treat newline as
          end of line. The ^ assertion, which normally would only match the
          beginning of the string, now match sees a match immediately following
          at newline. The same for the $ end-of-line assertion.

          The (.+?) parts mean capturing at least one character, with greediness
          turned off. Greediness dictates whether the regexp engine will try to
          find the longest possible match. We don't want it to be greedy here.
          Given the string "Time: 8:00PM", for example, we want the engine to
          match up the first colon and stop, instead of trying to find another
          colon that'd yield a longer match.

          In an actual programming situation I would use the following pattern to
          deal with possible errant white spaces:

          /^\s*(.+?)\s*:\s *(.+?)\s*$/m

          Comment

          • Damien

            #6
            Re: RegExp : using end-of-line in a pattern

            Chung Leong a écrit :[color=blue]
            > I would recommend using the Perl compatible regexp functions over the
            > POSIX ones. They're faster and more powerful. People also tend to be
            > more familiar with the Perl syntax.[/color]
            Hi,
            Thanks for this tip... I had gone for the first one in the PHP doc ;)
            [color=blue]
            >
            > The following snippet does what you want:[/color]
            (snip)

            Thanks for the code and espescially for the time you took explaining it!
            This will make me progress quite faster now ;)

            I'll try you suggestion tomorrow first thing. I'll be glad to get rid of
            all those substr and stristr...

            Thanks again !
            BR,
            Damien

            Comment

            Working...