Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Regex help

Expand Messages
  • Jenda Krynicky
    From: Malcolm Mill ... Try to read perlretut . Either run perldoc perlretut or go to
    Message 1 of 7 , Nov 3, 2004
    • 0 Attachment
      From: Malcolm Mill <malcolm.mill@...>
      > Hi Jenda,
      > The book I'm working from is "Apache, MySQL, and PHP Web Development:
      > For Dummies". It is a 7 books in 1 title, and has sections on Perl and
      > Regular Expressions. I looked for explicit examples of what I want to
      > to but couldnt find any so have been messing around with given
      > examples to try get the result I want.

      Try to read "perlretut".
      Either run
      perldoc perlretut
      or go to http://www.perldoc.com/perl5.8.4/pod/perlretut.html

      > > > print $_;
      > >
      > > If you are searching for something you should not mangle the data,
      > > just capture what do you need:
      > >
      > > /\t(\d{2}:\d{2})\s/ and print "$1\n";
      > >
      > >
      > > > }
      > >
      >
      > This code, from another list does what I eventually wanted to do,
      > which was capture not only d2 numbers, but d1,2 and [ap]m.
      >
      > foreach (<>) {
      > print "$1\n" while ($_ =~ /(\d{1,2}\:\d{2} [ap]m)/gi);
      > }
      >
      > What exactly do { } and g do?

      The {} specifies that the number(s) within specify how many
      occurances of the previous character or group to match.

      > I understand \d{1,2} means capture digits of length 1 or 2. Why
      > couldnt I use [ ]? Are they only for characters?

      Inside [] most special characters are not special anymore. The []
      specifies a group of characters (a character class) that may be
      accepted at the current place. This means that
      [\d2]
      means "match either a digit or 2" and is equivalent to
      \d
      or
      [0-9]
      or
      [0123456789]
      Also
      [\d{1,2}]
      is equivalent to
      [}{,\d]
      that is it means "match an opening or closng curly or a comma or a
      digit.

      Also /[:]/ is the same as /:/, /[\t]/ is the same as /\t/, /[\s]/ is
      the same as /\s/.

      So if I just simplified your regexp
      s/([\t][\d2][:][\d2][\s])/\1/;
      I'd get
      s/(\t\d:\d\s)/\1/;

      Jenda
      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery
    Your message has been successfully submitted and would be delivered to recipients shortly.