Help with regular expression please

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

    Help with regular expression please

    Trying to parse some strings and I could really use some help with a regular
    expression (and how to test it in php).

    My sample strings:

    Bob 1/0/X0 134 128 1 5 Data
    A comment line
    Bob 1/0/X2 84 82 25 0 COMPARTMENT1-3
    Bob 1/0/X1 1710 167 0 4 123 N/A
    Bob 11/0/X3 84 7 0 6 Data

    I'm trying to identify any string that starts with "Bob", then a space, then
    a digit OR a space, then #/#/X#, three spaces, a 1 to 4 digit number, some
    spaces, a 1 to 4 digit number, some spaces, a 1 to 4 digit number, some
    spaces, a 1 to 4 digit number, some spaces, some alpha characters.

    In my sample, the first, third and fifth strings would test positive.

    I'm sure I've botched this up, but this is what I have so far.

    if (
    ereg('^Bob..[0-9]/[0-9]/X[0-9]\b[0-9]{1,4}(.{9,13})[0-9]{2,4}(.{9,13})[A-Za-z]*'
    , $line ) ) { ...mycode... }


  • Rik

    #2
    Re: Help with regular expression please

    On Fri, 20 Jul 2007 11:11:42 +0200, Noozer <dont.spam@me.h erewrote:
    Trying to parse some strings and I could really use some help with a
    regular
    expression (and how to test it in php).
    >
    My sample strings:
    >
    Bob 1/0/X0 134 128 1 5 Data
    A comment line
    Bob 1/0/X2 84 82 25 0 COMPARTMENT1-3
    Bob 1/0/X1 1710 167 0 4 123 N/A
    Bob 11/0/X3 84 7 0 6 Data
    >
    I'm trying to identify any string that starts with "Bob", then a space,
    then
    a digit OR a space, then #/#/X#, three spaces, a 1 to 4 digit number,
    some
    spaces, a 1 to 4 digit number, some spaces, a 1 to 4 digit number, some
    spaces, a 1 to 4 digit number, some spaces, some alpha characters.
    >
    In my sample, the first, third and fifth strings would test positive.
    >
    I'm sure I've botched this up, but this is what I have so far.
    >
    if (
    ereg('^Bob..[0-9]/[0-9]/X[0-9]\b[0-9]{1,4}(.{9,13})[0-9]{2,4}(.{9,13})[A-Za-z]*'
    , $line ) ) { ...mycode... }
    >

    preg_match('%
    ^Bob #start of string
    \s+ #arbitrary whitespace
    [0-9]{1,2} #1 or 2 digits
    / #literal
    [0-9] #digit
    /X #literal
    [0-9] #digit
    \s+ #arbitrary whitespace
    [0-9]+ #digit(s)
    \s+ #arbitrary whitespace
    [0-9]+ #digit(s)
    \s+ #arbitrary whitespace
    [0-9]+ #digit(s)
    \s+ #arbitrary whitespace
    [0-9]+ #digit(s)
    \s+ #arbitrary whitespace
    .+ #rest of data
    %ix',$line);

    Then again: http://www.php.net/sscanf

    $lineparts = sscanf($line,'B ob %d/%d/X%d %d %d %d %d %s');
    --
    Rik Wasmus

    Comment

    • Alan

      #3
      Re: Help with regular expression please


      "Noozer" <dont.spam@me.h erewrote in message
      news:i%_ni.1326 46$1i1.126404@p d7urf3no...
      Trying to parse some strings .... My sample strings:
      >
      Bob 1/0/X0 134 128 1 5 Data
      A comment line
      Bob 1/0/X2 84 82 25 0 COMPARTMENT1-3
      Bob 1/0/X1 1710 167 0 4 123 N/A
      Bob 11/0/X3 84 7 0 6 Data
      >
      I'm trying to identify any string that starts with "Bob", then a space,
      then a digit OR a space, then #/#/X#, three spaces, a 1 to 4 digit number,
      some spaces, a 1 to 4 digit number, some spaces, a 1 to 4 digit number,
      some spaces, a 1 to 4 digit number, some spaces, some alpha characters.
      >
      In my sample, the first, third and fifth strings would test positive.
      preg_match('%^B ob\s+\d{1,2}/\d/X\d\s+\d{1,4}\s +\d{1,4}\s+\d{1 ,4}\s+\d{1,4}\s +[a-z]+.+%ix',$exstr) ;

      I think previous reply may also match your example 4

      hth
      Alan


      Comment

      Working...