regular expression inquiry

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

    regular expression inquiry

    I have a text file (see below please) to parse with the following code. i
    don't know why it cannot match anything. thanks.


    $data=fread(fop en("$dir/$file","r"),fil esize("$dir/file"));

    $matchno = preg_match_all( "|.*:(.+)\n$|U" , $data, $reg);

    for ($i=1; $i<$matchno; $i++) {
    $line = ",'".trim($ regs[1][$i]."'");
    fwrite($fp, $line);
    }


    GROUP: CARS R/C SERIES

    PRODUCT
    NAME: R/C CAR

    PACKAGE: WINDOW BOX

    SIZE:30.5¡Á12¡Á 10.6 cm

    MEAS:84* 34*93 cm

    PCS/CTN: 48 pcs

    GROSS
    WEIGHT: NIL

    NET
    WEIGHT: NIL

    INNER
    BOX QTY: NIL


  • Palle Hansen

    #2
    Re: regular expression inquiry

    vito wrote:[color=blue]
    > I have a text file (see below please) to parse with the following code. i
    > don't know why it cannot match anything. thanks.
    >
    >
    > $matchno = preg_match_all( "|.*:(.+)\n$|U" , $data, $reg);[/color]

    In single line mode $ will match the end of the entire text. This mode
    is default. |Um will switch to multi line mode

    In single line only \n is needed
    In multi line only $ is needed

    Comment

    • vito

      #3
      Re: regular expression inquiry

      >> $matchno = preg_match_all( "|.*:(.+)\n$|U" , $data, $reg);[color=blue]
      >
      > In single line mode $ will match the end of the entire text. This mode is
      > default. |Um will switch to multi line mode
      >
      > In single line only \n is needed
      > In multi line only $ is needed[/color]

      For irregularity of files (created by users), i need to first make the file
      content into a single line

      preg_replace("/\n/", "", $data);

      and then perform the matching.

      however, i found the replacment fails. maybe that is due to microsoft
      newline ^M instead of \n? if so, how to represent ^M in the pattern
      matching? furthermore, the newline assumption may be wrong and how can i
      know what the characters are there in the white spaces? I cannot use all
      space characters since the data also contain spaces.

      Thanks a lot.


      Comment

      Working...