to parse a string

Discussion in 'Perl Misc' started by cc, Oct 28, 2005.

  1. cc

    cc Guest

    Hi,

    I am new in Perl. Here is my question for help.

    Given a string, e.g. "01010123", any sample code to scan it and then
    find the "2" and "3" are not qualified in the string which requires
    only "0" and "1"?

    Thanks,

    mzc.
     
    cc, Oct 28, 2005
    #1
    1. Advertising

  2. "cc" <> wrote in news:1130456196.081874.32430
    @g44g2000cwa.googlegroups.com:

    > I am new in Perl. Here is my question for help.


    Then this is a good time to read the posting guidelines for this group
    to learn how you can help yourself, and help others help you.

    Please note that "write my code for me" requests are not very popular
    here.

    > Given a string, e.g. "01010123", any sample code to scan it and then
    > find the "2" and "3" are not qualified in the string which requires
    > only "0" and "1"?


    Read about character classes and regular expressions:

    perldoc perlre

    perldoc perlreref

    perldoc perlop

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 28, 2005
    #2
    1. Advertising

  3. cc

    cc Guest

    > > I am new in Perl. Here is my question for help.
    >
    > Then this is a good time to read the posting guidelines for this group
    > to learn how you can help yourself, and help others help you.
    >
    > Please note that "write my code for me" requests are not very popular
    > here.


    Clearly, this is not what I wanted either; and this is why my sample
    question from.

    > > Given a string, e.g. "01010123", any sample code to scan it and then
    > > find the "2" and "3" are not qualified in the string which requires
    > > only "0" and "1"?

    >
    > Read about character classes and regular expressions:
    >
    > perldoc perlre
    >
    > perldoc perlreref
    >
    > perldoc perlop


    I tried them before this email. Thank you anyway; can anybody else let
    me know where I can find those kind of good general sample codes?
    Thanks.
     
    cc, Oct 28, 2005
    #3
  4. "cc" <> wrote in
    news::

    >> > I am new in Perl. Here is my question for help.

    >>
    >> Then this is a good time to read the posting guidelines for this
    >> group to learn how you can help yourself, and help others help you.
    >>
    >> Please note that "write my code for me" requests are not very popular
    >> here.

    >
    > Clearly, this is not what I wanted either; and this is why my sample
    > question from.


    I am not sure what you mean by the above sentence, but you did not
    include any code.

    > let me know where I can find those kind of good general sample codes?


    You'd be better off trying to do it, and asking for help with specific
    issues.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Oct 28, 2005
    #4
  5. cc

    Guest

    If you have perl on your system, try to print out the
    perlcheat.html. On there there are Regex metachars.
    the [] denote a character class, so if [1a6], if any of these
    individual
    characters are contained in what yer checking it is a match.
    also, '-' is a metachar inside a [] meaning a span of chars
    so [a-z] is all the chars from a through z.

    You want to match 2 and/or 3 there, but not 0 or 1. So you want
    to match [2-9]. Put a couple of forward slashes around it and
    you have a regex. You might want to read up on it.

    use strict;
    use warnings;

    my $numstr = '01010123';

    if ($numstr =~ /[2-9]/) {
    print "bad numbers: $numstr\n";
    $numstr =~ s/[2-9]/-/g;
    print "replaced: $numstr\n";
    }

    __DATA__

    bad numbers: 01010123
    replaced: 010101--
     
    , Oct 28, 2005
    #5
  6. cc <> wrote:

    > Given a string, e.g. "01010123", any sample code to scan it and then
    > find the "2" and "3" are not qualified in the string which requires
    > only "0" and "1"?



    The usual idiom for validating data is:

    anchor the beginning.

    anchor the ending.

    write a regex in between that accounts for everything
    that you want to allow.


    print "'$str' is bad data\n" unless $str =~ /^[01]+$/;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 28, 2005
    #6
  7. cc

    Guest

    On Thu, 27 Oct 2005 21:59:01 -0500, Tad McClellan
    <> wrote:

    Well ya know Tad, i wanted to slightly introduce him to
    metacharacters [] since he might get scared of all the
    professionalism around hear

    >cc <> wrote:
    >
    >> Given a string, e.g. "01010123", any sample code to scan it and then
    >> find the "2" and "3" are not qualified in the string which requires
    >> only "0" and "1"?

    >
    >
    >The usual idiom for validating data is:
    >
    > anchor the beginning.
    >
    > anchor the ending.
    >
    > write a regex in between that accounts for everything
    > that you want to allow.
    >
    >
    >print "'$str' is bad data\n" unless $str =~ /^[01]+$/;
     
    , Oct 28, 2005
    #7
  8. Also sprach Tad McClellan:

    > cc <> wrote:
    >
    >> Given a string, e.g. "01010123", any sample code to scan it and then
    >> find the "2" and "3" are not qualified in the string which requires
    >> only "0" and "1"?

    >
    >
    > The usual idiom for validating data is:
    >
    > anchor the beginning.
    >
    > anchor the ending.


    Why the anchoring?

    > write a regex in between that accounts for everything
    > that you want to allow.
    >
    >
    > print "'$str' is bad data\n" unless $str =~ /^[01]+$/;


    Or use tr:

    print "'$str' is bad data\n" if $str =~ tr/^01//c;

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Oct 28, 2005
    #8
  9. Tassilo v. Parseval wrote:
    > Also sprach Tad McClellan:
    >
    >>cc <> wrote:
    >>
    >>>Given a string, e.g. "01010123", any sample code to scan it and then
    >>>find the "2" and "3" are not qualified in the string which requires
    >>>only "0" and "1"?

    >>
    >>The usual idiom for validating data is:
    >>
    >> anchor the beginning.
    >>
    >> anchor the ending.

    >
    > Why the anchoring?
    >
    >> write a regex in between that accounts for everything
    >> that you want to allow.
    >>
    >>
    >>print "'$str' is bad data\n" unless $str =~ /^[01]+$/;

    >
    > Or use tr:
    >
    > print "'$str' is bad data\n" if $str =~ tr/^01//c;

    ^
    Are you sure that caret is supposed to be there?


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Oct 28, 2005
    #9
  10. cc

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > Tassilo v. Parseval wrote:
    > > Also sprach Tad McClellan:
    > >
    > >>cc <> wrote:
    > >>
    > >>>Given a string, e.g. "01010123", any sample code to scan it and then
    > >>>find the "2" and "3" are not qualified in the string which requires
    > >>>only "0" and "1"?
    > >>
    > >>The usual idiom for validating data is:
    > >>
    > >> anchor the beginning.
    > >>
    > >> anchor the ending.

    > >
    > > Why the anchoring?
    > >
    > >> write a regex in between that accounts for everything
    > >> that you want to allow.
    > >>
    > >>
    > >>print "'$str' is bad data\n" unless $str =~ /^[01]+$/;

    > >
    > > Or use tr:
    > >
    > > print "'$str' is bad data\n" if $str =~ tr/^01//c;

    > ^
    > Are you sure that caret is supposed to be there?


    Yes, in Middle-High-Perl where double negation is the norm.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Oct 28, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vincent

    parse string

    Vincent, Dec 16, 2003, in forum: Perl
    Replies:
    2
    Views:
    2,829
    Gunnar Hjalmarsson
    Dec 16, 2003
  2. Kevin Joseph
    Replies:
    5
    Views:
    4,392
    Joe Smith
    Aug 13, 2004
  3. Henri
    Replies:
    2
    Views:
    327
    Henri
    Nov 29, 2004
  4. Replies:
    19
    Views:
    1,169
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    206
    7stud --
    Feb 20, 2008
Loading...

Share This Page